With Spring Boot 2.1 bean overriding is disabled by default, which is a good thing.
However I do have some tests where I replace beans with mocked instances using Mo
I make the testing beans available only in test
profile, and allow overriding for just while testing, like this:
@ActiveProfiles("test")
@SpringBootTest(properties = {"spring.main.allow-bean-definition-overriding=true"})
class FooBarApplicationTests {
@Test
void contextLoads() {}
}
The bean I am mocking in the test configuration:
@Profile("test")
@Configuration
public class FooBarApplicationTestConfiguration {
@Bean
@Primary
public SomeBean someBean() {
return Mockito.mock(SomeBean.class);
}
}