I want to test my scheduled task, so I followed this tutorial
@SpringJUnitConfig(SchedulerConfig.class)
public class MailJobFinderTaskIT {
@SpyBean
private MailJobFinderTask mailJobFinderTask;
@Test
public void whenWaitThreeSecond_ThenTaskCalledThreeTimes(){
await()
.atMost(Duration.ofSeconds(3))
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs());
}
}
But actually it does not work, because the the following error
org.mockito.exceptions.misusing.NullInsteadOfMockException:
Argument passed to verify() should be a mock but is null!
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
not: verify(mock.someMethod());
Also, if you use @Mock annotation don't miss initMocks()
here is the signature of my Task-Class
@Component
public class MailJobFinderTask extends SuppressedLogPoller {
....
}
@Scheduled(fixedRate = 1000)
public void findEmailJobs() {
.
.
}
I tried already to change the Annotation to @SpringBootTest and also tried to used @MockBean instead of @SpyBean but without any success. Actually I do not understand why my bean mailJobFinderTask is not created