Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why prefering @TestConfiguration than @Configuration in src/test

i currently study more in deep the @TestConfiguration and i wonder why using this combo..

@TestConfiguration
public class TestConfig {
    
    // some bean definitions 

and then using

@SpringBootTest
@Import(TestConfig.class)
class UsingImport {

@Autowired
ConfigBean configBean;

// some code 

and not using the classic @Configuration, in the src/test, as it is totally legal do it. Why is the real purpose of @TestConfiguration, as we can do the same with @Configuration ?

Am i wrong ?

like image 791
work-in-progress Avatar asked Dec 29 '25 22:12

work-in-progress


1 Answers

The key difference between @TestConfiguration and regular @Configuration is that the classes marked with the usual @Configuration annotation are included in the application context automatically because scanning recognises this annotation. Whereas classes labelled with @TestConfiguration are not subject to scanning, and you will have to import the configuration for each test manually, giving you the flexibility to control the number of beans needed for that test

like image 123
Сестренка 8 лет Avatar answered Dec 31 '25 18:12

Сестренка 8 лет