I am trying to use the @MockBean annotation in my Spring Batch Test, but I am unable to get it to work. I have simplified my test to it's most basic form for this post. Essentially, @Autowired works (so I know the class is being picked up by Spring), but when I replace it with the @MockBean annotation, the bean is not mocked (it's null). Any idea what I could be doing wrong?
This works fine.
@SpringBatchTest
@WebAppConfiguration
@ContextConfiguration(classes = {TestAppConfig.class})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@TestPropertySource("classpath:application.properties")
public class EmailQueueBatchConfigTestFail {
@Autowired
private AppUserEmailSender appUserEmailSender;
@Test
public void test() {
if (appUserEmailSender == null) {
System.out.println("null");
}
}
}
However, the mocked object here is null
@SpringBatchTest
@WebAppConfiguration
@ContextConfiguration(classes = {TestAppConfig.class})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@TestPropertySource("classpath:application.properties")
public class EmailQueueBatchConfigTestFail {
@MockBean
private AppUserEmailSender appUserEmailSender;
@Test
public void test() {
if (appUserEmailSender == null) {
System.out.println("null");
}
}
}