是否有其他方法可以为以下情况编写测试用例,需要为 runStudentService 方法编写测试用例。尝试编写如下测试用例,但抛出:“想要但未调用 - 实际上,与此模拟的交互为零。”
@Component
public class StudentScheduler {
@Autowired
private StudentService studentService;
@Scheduled(cron = "${cron.students}")
public void runStudentService() {
try {
studentService.startStudetsTest();
} catch (Exception e) {
LOG.error("Error occured during test" + e);
throw(e);
}
}
}
@RunWith(SpringJUnit4ClassRunner.class)
public class StudentSchedulerTest {
@Mock
private StudentService studentService;
StudentScheduler scheduler = Mockito.mock(StudentScheduler.class);
@Test
public void jobRuns() {
Mockito.doNothing().when(scheduler).runStudentService();
verify(scheduler, Mockito.times(1)).runStudentService();
}
}