I'm using Spring Context (v5.2.22 and Java8) and Spring Test in a base framework (consumed by other apps from my team).
To facilitate the setup of tests, I have created a BaseTestContextConfiguration class, containing some common mocked beans, some properties and also some @ComponentScan, aimed to be reused on every test scenarios of the children apps.
@ComponentScan("com.ci.common.commands")
public class BaseTestContextConfiguration {
@Bean("JcommanderBean")
@Primary
public Properties getJcommanderProperties() {
return new Properties();
}
}
But when I try to reuse it the way bellow, creating a new Configuration class (extending from BaseTestContextConfiguration) and passing it to @ContextConfiguration, it is not working as expected. The component scanning defined on parent BaseTestContextConfiguration is not being activated.
@ExtendWith({ SpringExtension.class })
@ContextConfiguration(classes = { ApplicationUnitTest.ContextCommandConfiguration.class})
class ApplicationUnitTest {
@PropertySource("classpath:test.properties")
public static class ContextCommandConfiguration extends BaseTestContextConfiguration {
@Bean
@Primary
public MockableAsyncTestTask getMockableTestTask() {
return Mockito.mock(MockableAsyncTestTask.class);
}
}
However, if I add the BaseTestContextConfiguration directly to the @ContextConfiguration then the scan works, but this prevent me to override some bean declaration method defined on it.
@ExtendWith({ SpringExtension.class })
@ContextConfiguration(classes = { ContextCommandConfiguration.class, BaseTestContextConfiguration.class })
class ApplicationUnitTest {
@PropertySource("classpath:test.properties")
public static class ContextCommandConfiguration {
@Bean("mock")
@Primary
public MockableAsyncTestTask getMockableTestTask() {
MockableAsyncTestTask spied = Mockito.spy(MockableAsyncTestTask.class);
return spied;
}
}
I would like to understand the reason of my first approach haven't work, and also if there are more alternatives other than the one I used on my second approach.