Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of @TestConfiguration on a top-level class?

Spring Boot 1.4 has brought many good improvements for testing.

I am unclear of the purpose of @TestConfiguration. It doesn't seem to work as expected.

I thought that if I use @TestConfiguration on a top-level class under /src/test/java that it gets picked up by a @SpringBootTest test (see here). But this seems not to be the case. If I instead use @Configuration then it gets correctly picked up.

Did I misunderstand? Is the manual wrong? Is there a bug in the Spring Boot code?

like image 938
jhyot Avatar asked Sep 22 '16 14:09

jhyot


2 Answers

Since I've asked this question, the wording in the documentation has been clarified and explains the behaviour much better now.

Per the new documentation:

When placed on a top-level class, @TestConfiguration indicates that classes in src/test/java should not be picked up by scanning. You can then import that class explicitly where it is required, as shown in the following example:

@SpringBootTest
@Import(MyTestsConfiguration.class)
class MyTests {

    @Test
    void exampleTest() {
        // ...
    }

}
like image 100
jhyot Avatar answered Oct 31 '22 18:10

jhyot


whenever we have used <context:component-scan base-package="some.package" /> or @ComponentScan . it scans all . for that to prevent Spring Boot provides @TestComponent so that they should not be picked up by scanning.

like image 23
Sam Avatar answered Oct 31 '22 16:10

Sam