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 {
}
P.S. we saw the same behaviour when we used @Mockbean annotation