【问题标题】:Building a library using Spring Boot: Unable to find @SpringBootConfiguration使用 Spring Boot 构建库:找不到@SpringBootConfiguration
【发布时间】:2021-05-13 07:47:34
【问题描述】:

我目前正在研究微服务,这个项目基本上是一个库,它没有主要功能。我正在为这个库编写单元测试用例,但我找不到 @ SpringBoot 配置。如果没有main函数,我们不能执行单元测试用例吗?

库中唯一的文件

class OnlyFile {

    void validate(){

    }

}

测试

@SpringBootTest
class MyTest {

     @Test
     void testIt(){
        //
        //
      }
     
}

pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <version>2.3.3.RELEASE</version>
   <scope>test</scope>
   <exclusions>
     <exclusion>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
     </exclusion>
    </exclusions>
</dependency>

【问题讨论】:

    标签: java spring spring-boot junit


    【解决方案1】:

    如果您想编写和运行单元测试并且它只是一个库,那么我认为您不需要在测试类上使用 @SpringBootTest 注释。它用于集成测试,当您需要在集成测试中拥有 Spring 应用程序上下文时。

    在使用@SpringBootTest 注解的类中运行集成测试时,您需要有一个与使用@SpringBootConfiguration 注解的测试类在同一包或子包中的类,其中包含加载应用程序上下文的配置。如果您不需要自定义配置,则该类可以为空。或者,您可以像这样指定在加载应用程序上下文时使用哪些组件

    @SpringBootTest(classes = ...)
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 2017-08-17
      • 2017-04-26
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多