According to 25.3.3. Excluding Test Configuration this capability exists:
When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning
I created both @Configuration FilledWithBeanTestConfig
and @TestConfiguration DummyTestConfig
in the project's root path/package src/test/java/com/example/mvctries/
. My main
method-containing class is on purpose annotated with @ComponentScan
just in order to accidentally pick FilledWithBeanTestConfig
; about this situation Spring is specifically warning:
If your application uses component scanning (for example, if you use @SpringBootApplication or @ComponentScan), you may find top-level configuration classes that you created only for specific tests accidentally get picked up everywhere.
I used @Import(DummyTestConfig.class)
in a @WebMvcTest
annotated test class placed in src/test/java/com/example/mvctries/controller/
; I was expecting for the @Configuration FilledWithBeanTestConfig
to just be ignored but it wasn't. Moving DummyTestConfig
to src/test/java/com/example/mvctries/controller/
also didn't help.
What am I doing wrong when testing the way presented above this src/test/java
scanning-ignore capability?
How should I properly use @TestConfiguration
in order to use this src/test/java
scanning-ignore capability?
How would I test this capability by using @SpringBootApplication
component scanning only instead of on purpose scanning with @ComponentScan
?
While the above questions might seem too many they are in fact another way of asking the following one question: how would one craft a project which to clearly prove how @TestConfiguration
usage leads to classes in src/test/java should not be picked up by scanning.
UPDATE
Here's the example project: https://github.com/adrhc/spring-boot-test-checks.git; it's using port 8083 when running the application; better run the test class.