For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the R
@MockBean
and bean overriding used by the OP are two complementary approaches.
You want to use @MockBean
to create a mock and forget the real implementation : generally you do that for slice testing or integration testing that doesn't load some beans which class(es) you are testing depend on and that you don't want to test these beans in integration.
Spring makes them by default null
, you will mock the minimal behavior for them to fulfill your test.
@WebMvcTest
requires very often that strategy as you don't want to test the whole layers and @SpringBootTest
may also require that if you specify only a subset of your beans configuration in the test configuration.
On the other hand, sometimes you want to perform an integration test with as many real components as possible, so you don't want to use @MockBean
but you want to override slightly a behavior, a dependency or define a new scope for a bean, in this case, the approach to follow is bean overriding :
@SpringBootTest({"spring.main.allow-bean-definition-overriding=true"})
@Import(FooTest.OverrideBean.class)
public class FooTest{
@Test
public void getFoo() throws Exception {
// ...
}
@TestConfiguration
public static class OverrideBean {
// change the bean scope to SINGLETON
@Bean
@Scope(ConfigurableBeanFactory.SINGLETON)
public Bar bar() {
return new Bar();
}
// use a stub for a bean
@Bean
public FooBar BarFoo() {
return new BarFooStub();
}
// use a stub for the dependency of a bean
@Bean
public FooBar fooBar() {
return new FooBar(new StubDependency());
}
}
}