Issue
Spring 2.4.5.
Trying to run test examples.
2022-06-30 21:58:39.897 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest], using SpringBootContextLoader
2022-06-30 21:58:39.901 INFO --- [ Test worker] o.s.t.c.support.AbstractContextLoader : Could not detect default resource locations for test class [dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest]: no resource found for suffixes {-context.xml, Context.groovy}.
2022-06-30 21:58:39.902 INFO --- [ Test worker] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest]: StatusRepositoryIntegrationTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2022-06-30 21:58:39.961 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration company.status.dashboard.api.App for test class dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest
2022-06-30 21:58:40.013 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [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.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, 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.test.context.event.EventPublishingTestExecutionListener]
2022-06-30 21:58:40.022 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@5be3295e, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@48ec863b, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@37762e2a, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@4db30010, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@f586921, org.springframework.test.context.support.DirtiesContextTestExecutionListener@23a9fbaf, org.springframework.test.context.transaction.TransactionalTestExecutionListener@2528968e, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@70cea1bb, org.springframework.test.context.event.EventPublishingTestExecutionListener@3c93dfe2, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@500b2f5c, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@31f64f35, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@3430e3c9, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@16fe5662, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@1f06ea2e, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1c8a911c]
2022-06-30 21:58:40.025 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest$FindFirstDailyDeliveryStatusesByDatasetUnitWithinTimeRange], using SpringBootContextLoader
2022-06-30 21:58:40.025 INFO --- [ Test worker] o.s.t.c.support.AbstractContextLoader : Could not detect default resource locations for test class [dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest$FindFirstDailyDeliveryStatusesByDatasetUnitWithinTimeRange]: no resource found for suffixes {-context.xml, Context.groovy}.
2022-06-30 21:58:40.026 INFO --- [ Test worker] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest$FindFirstDailyDeliveryStatusesByDatasetUnitWithinTimeRange]: FindFirstDailyDeliveryStatusesByDatasetUnitWithinTimeRange does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2022-06-30 21:58:40.027 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration company.status.dashboard.api.App for test class dataset.status.dashboard.api.repository.StatusRepositoryIntegrationTest$FindFirstDailyDeliveryStatusesByDatasetUnitWithinTimeRange
2022-06-30 21:58:40.029 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [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.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, 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.test.context.event.EventPublishingTestExecutionListener]
2022-06-30 21:58:40.029 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@14f0dd2a, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6c6b8304, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@370431a5, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@491f8897, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@5b140513, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6b57ced6, org.springframework.test.context.transaction.TransactionalTestExecutionListener@7dc84300, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@48a6371e, org.springframework.test.context.event.EventPublishingTestExecutionListener@33e18f84, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@6695c650, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@59fa49c1, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@201d9b27, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@1f3fbd0f, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@57d05ffe, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@5cefa084]
2022-06-30 21:58:40.183 INFO 69695 --- [ Test worker] .StatusRepositoryIntegrationTest : Starting StatusRepositoryIntegrationTest using Java 11.0.15 on C12346 with PID 69695 (started by alex in /Users/alex/projects/status-dashboard-api)
2022-06-30 21:58:40.183 INFO 69695 --- [ Test worker] .StatusRepositoryIntegrationTest : The following profiles are active: local
Configuration for tests is being taken from main.java.resources.application-local.properties
, whereas it must be taken from test.java.resources
folder.
I tried different options SPRING.PROFILES.ACTIVE
, spring.profiles.active
, SPRING_PROFILES_ACTIVE
with value test
in IntelliJ Idea's Run/Debug Configurations
window but it did help.
Test I want to run has the following annotations:
@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestPropertySource(locations = {"classpath:application-integration.properties"})
class StatusRepositoryIntegrationTest {
Thank you.
Solution
spring.profiles.active
is means what will be currently active if you not configurated another. If you would like to run with another profile, see this.
Of course, if you configurate another profile, you have to application-<profile_name>.properties/yml
You don't need @TestPropertySource
because Spring will find it under test and main folders. If there are two files with the same name in the main and in the test, I have noticed that they overwrite each other. I did not test whether the configuration is overridden as a complete file or only extend by test.
If you only want to run a test or apply a setting to a specific profile, then you need a @ActiveProfile("profile").
To learn more about profiles, read this.
If we look at the example mentioned.
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfile("integration")
class StatusRepositoryIntegrationTest { ... }
mvn/gradle test command -Dspring.profiles.active=integration
or --spring.profiles.active=integration
Answered By - Numichi
Answer Checked By - Senaida (JavaFixing Volunteer)