This issue is currently being observed in version 3.4.3. Here is quick bit of code to highlight issue
@SpringBootTest
class MockObjectTest {
@Test
void doNothing() {
// test started up fine
}
@TestConfiguration
static class InitMocks {
@MockBean // deprecated and set for removal in 3.6.0
private SomeObject mockBean;
@MockitoBean
private SomeObject mockitoBean;
@PostConstruct
public void init() {
assertNotNull(mockBean); // successful
assertNotNull(mockitoBean); // fails
}
}
}
Comment From: wilkinsona
@TestConfiguration is meta-annotated with @Component which is why this works with @MockBean. @MockitoBean doesn't support such injection. https://github.com/spring-projects/spring-framework/issues/34415 has a suggestion for an alternative arrangement that may help.
Comment From: umekong
Should the Javadoc be updated to reflect that MockitoBean is not a like-for-like replacement for the deprecated MockBean then?
Also @wilkinsona, I would need to verify but the suggestion in https://github.com/spring-projects/spring-framework/issues/34415 would not work in scenarios where the TestConfiguration class has been introduced because the MockitoBean needs to be configured before usage by another Bean within the Spring context.
Comment From: wilkinsona
https://github.com/spring-projects/spring-boot/issues/43348 is tracking some documentation improvements.
the
MockitoBeanneeds to be configured before usage by another Bean within the Spring context.
With that requirement, whether you're using @MockBean or @MockitoBean, we recommend using a @Bean method that creates, configures, and returns a Mockito mock directly instead.