@SpringBootTest без объявления всех необходимых классов - PullRequest
0 голосов
/ 05 апреля 2019

На данный момент мне нужно сообщить аннотации @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

1 Ответ

0 голосов
/ 05 апреля 2019

Я думаю, что Spring Boot не обнаружил классы.Проверьте, соответствует ли ваш путь основных тестовых классов вашим классам, которые вы хотите проверить.

Если ваш класс находится здесь:

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

, то путь к тестовому классу должен выглядеть следующим образом:

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

Если путь не такой, вы импортируете классы в SpringBootTest с классами = ....

...