java - SpringBoot : Running a Junit unit test with minimal configuration

标签 java spring-boot junit spring-data-jpa spring-junit

我有一个 Springboot 项目,在该项目中,我找到了一种创建和运行简单的 Junit 测试用例的方法,该测试用例查看存储库并获取给定实体的一些数据属性。 Junit 运行的结果是通过,因此没有问题。

但问题是,我看到了很多示例,其中教程展示了 Springboot 项目,在这些项目中,他们可以仅使用 运行 Junit 测试>@Runwith@SpringBootTest 针对他们的特定测试类(class)。

就我而言,我必须添加 3 个注释,@SpringBootTest@RunWith 以及 @ContextConfiguation(带参数) 直到我能够运行测试用例。

所以我的问题是我如何才能尽可能简单地运行它,(我见过的一些练习的 springboot 测试类只有一个注释)

我的 Springboot 测试类如下所示:

Screenshot of my Junit class

我的目录结构如下所示:

Screenshot of my Project directory structure

我的application.properties看起来像这样:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/erfan
spring.datasource.username=erfan
spring.datasource.password=

#Some additional properties is trying to be set by Spring framework so this must be set
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true


#spring.datasource.initialization-mode=always
#spring.datasource.initialize=true
#spring.datasource.schema=classpath:/schema.sql
#spring.datasource.continue-on-error=true 

#HikariCP is a ConnectionPool manager, related to DB stuff



#Below is the property key you need to set to * as value to expose all kind of monitoring related information
#about your web application 

#management.endpoints.web.exposure.include=*

还有我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>
    <groupId>com.sample</groupId>
    <artifactId>postgres</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>postgres</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>



    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>



        </plugins>






    </build>
</project>

那么我是否缺少 application.properties 文件中的某些内容?我应该包含一些内容才能删除测试类中的“样板”注释?

最佳答案

取决于您想要做什么。基本上,spring 具有自定义注释,可将 spring 上下文配置为仅包含相关 bean。这就是所谓的test slices .

但是我总是尝试遵循一些“规则”:

  • 避免 @SpringBootTest除非您正在进行集成测试,或手动设置要使用的类 @SpringBootTest(classes = {MyService1.class, MyService2.class}
  • 如果您正在测试 spring jpa,则可以使用 @DataJpaTest注释,示例 here
  • 如果您正在测试 Controller ,则可以使用 @WebMvcTest ,例如here
  • 如果您正在测试其他服务,您可以随时使用 @ContextConfiguration相应地配置 spring 上下文。

例如,对于您的测试,我会用以下两种方式之一编写它:

@RunWith(SpringRunner.class)
@DataJpaTest
@Import(AnotherConfig.class)
class MyTest {
   // test stuff here
}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AnotherConfig.class})
// NOTE, if you have a JpaConfig class where you @EnableJpaRepositories
// you can instead add this config class to the @ContextConfiguration classes
@EnableJpaRepositories
class MyTest {
   // test stuff here
}

基本上,不用担心测试上有多少注释,而是担心哪些 bean/服务正在被 Autowiring 。例如@SpringBootTest是单个注释,但会 Autowiring Spring 上下文中的所有 bean。

关于java - SpringBoot : Running a Junit unit test with minimal configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60113060/

相关文章:

java - 在基于 GWT 的 Web 应用程序中查看 3D STEP .stp CAD 模型

java - DAO/存储库 : Good practice return value after insert/update

java - 跟踪谁在微服务中创建或更改了实体

java - 使用 powermockito 验证静态 void 方法的调用

ios - 在 Jenkins 上运行 Xcode UI 测试永远不会失败

java - 发送带有附件的邮件 - angularJS + Java Spring

java - 不使用 java.io 写入磁盘

java - 测试中使用 Quartz 时出现 NoSuchBeanDefinitionException

reactjs - 我应该如何在我的案例中使用 OAuth 2.0 授权授权(React 前端,Spring Boot 后端)

java - RestController 的 Junit - 断言错误