3

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 ?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

i made a test but it seems it's not right : i used a bean from a @TestConfiguration and it was used as injection, but at the same time, i could use a bean from the default configuration (annoted by @Configuration). So, it seems that the default application context is effectively raised. Pehraps there is something i don't understand, or an option i miss..
Try looking here reflectoring.io/spring-boot-testconfiguration I apologise for misinforming you, the overall context of the application is still raised. Edited reply
0

@TestConfiguration:

@Configuration that can be used to define additional beans or customizations for a test. Unlike regular @Configuration classes the use of @TestConfiguration does not prevent auto-detection of @SpringBootConfiguration.

1 Comment

so, the only purpose of @TestConfiguration, for the other which do not use/import it, to not scan the declarations of the beans which are inside . that's all ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.