Java @SpringBootTest不声明所有需要的类

Java @SpringBootTest不声明所有需要的类,java,spring-boot,spring-boot-test,Java,Spring Boot,Spring Boot Test,目前,我需要告诉@SpringBootTest注释我需要哪些类来进行so测试。如果我不这样做,那么在测试应用程序上下文中找不到这些类。除了在每个测试中声明每个类之外,我只想提供某种配置类,它定义了测试所需的所有类 目前,测试正在使用以下注释: @EnableAutoConfiguration @SpringBootTest( properties = {"spring.jpa.hibernate.naming.implicit_strategy: org.hibernate.boot.m

目前,我需要告诉@SpringBootTest注释我需要哪些类来进行so测试。如果我不这样做,那么在测试应用程序上下文中找不到这些类。除了在每个测试中声明每个类之外,我只想提供某种配置类,它定义了测试所需的所有类

目前,测试正在使用以下注释:

@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
    }
)
我试图创建一个配置类,并从@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

我认为SpringBoot还没有发现这些类。 检查TestClass的路径是否和您想要测试的类一致

如果您的班级在这里:

/src/main/java/com/example/app/MainApplication.java
然后,测试类的路径必须如下所示:

/src/test/java/com/example/app/MainApplicationTest.java
如果路径与此不同,则必须将类导入到类为的SpringBootTest