目前,我需要告诉@SpringBootTest批注我需要测试哪些类。如果我不这样做,则在测试应用程序上下文中找不到这些类。除了在每个测试中声明每个单独的类之外,我只想提供某种配置类,该配置类定义了测试所需的所有类。
目前该测试正在使用以下注释:
@EnableAutoConfiguration
@SpringBootTest(
properties = {"spring.jpa.hibernate.naming.implicit_strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl"},
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {
CategoryController.class,
CategoryService.class,
CategoryValidationService.class,
CategoryNameLengthValidator.class,
CategoryPositionUniquenessValidator.class,
GlobalExceptionHandler.class
}
)
我试图创建一个Configuration类,并从@SpringBootTest批注中删除了这些类,但是却找不到这些类:
@EnableAutoConfiguration
@SpringBootTest(
properties = {"spring.jpa.hibernate.naming.implicit_strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl"},
webEnvironment = WebEnvironment.RANDOM_PORT,
)
以及配置类:
@Configuration
@ComponentScan(basePackages = {"com.it.mypackage"})
public class ContextConfiguration {
}
但是如果我不告诉@SpringBootTest批注在测试过程中需要哪些类,那么它就行不通了。
出现的错误:
[main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.bbraun.cit.dlm.controller.CategoryControllerIT]: no resource found for suffixes {-context.xml, Context.groovy}.
[main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener]
[main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@5b7a8434, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@5c45d770, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@2ce6c6ec, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1bae316d, org.springframework.test.context.support.DirtiesContextTestExecutionListener@147a5d08, org.springframework.test.context.transaction.TransactionalTestExecutionListener@6676f6a0, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@7cbd9d24, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@1672fe87, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@5026735c, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@1b45c0e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@11f0a5a1, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@10f7f7de]
[background-preinit] WARN org.springframework.http.converter.json.Jackson2ObjectMapperBuilder - For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
答案 0 :(得分:0)
我认为Spring Boot尚未发现这些类。 检查您的TestClasses路径是否与您要测试的类相对应。
如果您的课程在这里:
/src/main/java/com/example/app/MainApplication.java
然后测试类的路径必须如下所示:
/src/test/java/com/example/app/MainApplicationTest.java
如果路径不是这样,您已经使用classes = ....导入了类到SpringBootTest。