use verify on mocked (with Mockito) objects inject by guice

£可爱£侵袭症+ 提交于 2019-12-04 21:57:49

This should work, did you debug through your code? Guice should invoke your provider to get an instance of DataManager. If not, how does it get instantiated? Put a break point there and find out.

PEBKAC problem, Guice and Mockito works perfectly, it's just that i launch job with quartz, so quartz launch the job in a new thread. And without waiting, i check if my mocked object were already called, so my problem.

So i change my test method with this

@Test
public void testDefaultJob() throws InterruptedException, Exception {
    JobDetail jobDetail = newJob().ofType(OneDayBatchStarter.class)
            .withIdentity(DAILY_DEFAULT_JOB, Scheduler.DEFAULT_GROUP).build();
    scheduler.scheduleJob(jobDetail, TriggerBuilder.newTrigger().startNow().build());
    Thread.currentThread().sleep(500);
    while (!scheduler.getCurrentlyExecutingJobs().isEmpty()) {
        Thread.currentThread().sleep(500);
    }
    verify(dataManager).getObjectCodeList();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!