0

是否有其他方法可以为以下情况编写测试用例,需要为 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();
    }
    
}

4

1 回答 1

0

您模拟的 StudentScheduler 类没有使用您模拟的 studentService 实例。仔细检查runStudentService() 函数中的studentService 实例与您的模拟studentService 实例是否相同。

将模拟的 studentService 实例传递给要调用的 runStudentService() 函数!

于 2021-08-30T01:29:20.407 回答