简体   繁体   中英

Spring boot test not using overridden bean in test in spite of marking @Primary

We have a bean CompanyProcessor. In spring boot integration test, we want to test an exception scenario when a method in this bean throws Exception.

Therefore we have created an overriding bean TestCompanyProcessor which extends CompanyProcessor. TestCompanyProcessor is marked @Primary. When we run just this test class, the test runs fine and the overridden bean is used as expected.

But then this test class is run with several other test classes together, each marked as @SpringBootTest, we see that this test fails at first instance as instead of using the overridden bean, it uses this original bean. We have automatic retries on failure for upto 3 retries. On the second or third retry, it somehow finds the correct overridden primary bean and the test passes.

Is there any reason, why it finds the original bean at first instance and then finds the correct overridden bean on subsequent retries when run alongside several other test classes.

The overriden bean is defined as follows.

@Primary
@Component
@ConditionalOnExpression("'${test.company.processor}' == 'true'")
class TestCompanyProcessor

and on the test class we have

@TestPropertySource(properties = {"test.company.processor=true"})
class TestCompanyProcessorTest {

}

PS we saw the same behaviour when we used @Mockbean annotation

You sees this behaviour probably if you have multiple Spring tests that share the same context.
Spring is caching your test application context between tests. This is the default behaviour as the instanciation of the objects in a spring context may take some time.

If you have different beans in different tests you should mark tests that change the ApplicationContext (like the TestCompanyProcessorTest class) with an @DirtiesContext annotation.

Further info on caching:
https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testing-ctx-management
DirtiesContext example:
https://www.baeldung.com/spring-dirtiescontext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM