简体   繁体   English

@SpringBootTest 没有声明每个需要的类

[英]@SpringBootTest without declaring every needed class

At the moment i need to tell the @SpringBootTest annotation which classes i need for so test.目前我需要告诉@SpringBootTest 注释我需要哪些类来进行测试。 If i won't do that, the classes are not found in the test application context.如果我不这样做,则在测试应用程序上下文中找不到这些类。 Except of declaring in every test each single class i just want to provide some kind of configuration class, which defines all needed classes for the test.除了在每个测试中声明每个类之外,我只想提供某种配置类,它定义了测试所需的所有类。

At the moment the test is working with the following annotations:目前,测试正在使用以下注释:

@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
    }
)

I tried to create an Configuration class and removed the classes from the @SpringBootTest annotation, but then the classes weren't found:我尝试创建一个 Configuration 类并从 @SpringBootTest 注释中删除了这些类,但是没有找到这些类:

@EnableAutoConfiguration
@SpringBootTest(
    properties = {"spring.jpa.hibernate.naming.implicit_strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl"},
    webEnvironment = WebEnvironment.RANDOM_PORT,
)

And the configuration class:和配置类:

@Configuration
@ComponentScan(basePackages = {"com.it.mypackage"})
public class ContextConfiguration {
}

But it didn't worked if i don't tell the @SpringBootTest annotation which classes i need during test.但是如果我不告诉 @SpringBootTest 注释我在测试期间需要哪些类,它就不起作用。

Error which appears:出现的错误:

[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

I think that Spring Boot has not discovered the classes.我认为 Spring Boot 还没有发现这些类。 Check if your path of the TestClasses coresponds with your Classes that you want test.检查您的 TestClasses 的路径是否与您要测试的 Classes 对应。

If your class is here:如果你的班级在这里:

/src/main/java/com/example/app/MainApplication.java

then the path to the test class must look like this:那么测试类的路径必须如下所示:

/src/test/java/com/example/app/MainApplicationTest.java

If the path is not like this you have import the classes to SpringBootTest with classes=....如果路径不是这样,您必须使用 classes=... 将类导入 SpringBootTest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM