Is it possible to replace an inherited @MockBean
with the real @Bean
?
I have an abstract class that defines many configurations and a setup for all ITests. Only for one single test I want to make use of the real bean, and not used the mocked one. But still inherit the rest of the configuration.
@Service
public class WrapperService {
@Autowired
private SomeService some;
}
@RunWith(SpringRunner.class)
@SpringBootTest(...)
public abstract class AbstractITest {
//many more complex configurations
@MockBean
private SomeService service;
}
public class WrapperServiceITest extends AbstractITest {
//usage of SomeService should not be mocked
//when calling WrapperService
//using spy did not work, as suggested in the comments
@SpyBean
private SomeService service;;
}
@Mock is used when the application context is not up and you need to Mock a service/Bean. @MockBean is used when the application context(in terms of testing) is up and you need to mock a service/Bean.
Spring Boot's @MockBean AnnotationThe mock will replace any existing bean of the same type in the application context. If no bean of the same type is defined, a new one will be added. This annotation is useful in integration tests where a particular bean, like an external service, needs to be mocked.
Spring Boot provides @MockBean annotation that can be used to define a Mockito mock for a bean inside our ApplicationContext , that means the mock declared in test class (by using @MockBean) will be injected as a bean within the application.
The @SpyBean is a Spring Boot test annotation that is used to add Mockito spies to ApplicationContext . 2. Spies can be applied by type or bean name. 3. All existing beans of the same type defined in the context will be wrapped with spy and if no existing bean then new one will be added to context.
Found a way using a test @Configuration
conditional on a property, and overriding that property in the impl with @TestPropertySource
:
public abstrac class AbstractITest {
@TestConfiguration //important, do not use @Configuration!
@ConditionalOnProperty(value = "someservice.mock", matchIfMissing = true)
public static class SomeServiceMockConfig {
@MockBean
private SomeService some;
}
}
@TestPropertySource(properties = "someservice.mock=false")
public class WrapperServiceITest extends AbstractITest {
//SomeService will not be mocked
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With