我使用mockito作为模拟框架.我在这里有一个scenerio,我的时间(abc.method()).thenReturn(value)不返回值,而是返回null.
public class DQExecWorkflowServiceImplTest { @InjectMocks DQExecWorkflowServiceImpl dqExecWorkflowServiceImpl = new DQExecWorkflowServiceImpl(); @Mock private DQUtility dqUtility; @Mock private DqExec dqExec; @Mock private DqCntlDefn dqCntlDefn; @Mock private DqCntlWfDefn dqCntlWfDefn; @Mock private DqCntlWfDefnTyp dqCntlWfDefnTyp; @Mock private IDQControlWfDefTypeService controlWfDefTypeService; @Before public void setUp() throws Exception { dqExec = new DqExec(); dqCntlWfDefn = new DqCntlWfDefn(); dqUtility = new DQUtility(); dqCntlWfDefnTyp = new DqCntlWfDefnTyp(); dqCntlWfDefnTyp.setDqCntlWfDefnTypCd("MIN_INCLUSIVE_VAL"); dqExecWorkflowServiceImpl .setControlWfDefTypeService(controlWfDefTypeService); } @Test public void testExecuteWorkflow() { when(controlWfDefTypeService.getDqCntlWfDefnTypCd(dqCntlWfDefn)) .thenReturn(dqCntlWfDefnTyp); dqExecWorkflowServiceImpl.executeWorkflow(dqExec, dqCntlWfDefn); }
}
Java类
@Override public DqCntlWfExec executeWorkflow(final DqExec dqExec, final DqCntlWfDefn dqCntlWfDefn) { final DqCntlWfExec dqCntlWfExec = new DqCntlWfExec(); dqCntlWfExec.setDqCntlWfExecEffDt(dqUtil.getDefaultEffectiveDt()); dqCntlWfExec.setDqCntlWfExecExpDt(dqUtil.getDefaultExpiryDt()); dqCntlWfExec.setDqCntlWfDefn(dqCntlWfDefn); dqCntlWfExec.setDqExec(dqExec); final DqCntlWfDefnTyp dqCntlWfDefnTyp = controlWfDefTypeService .getDqCntlWfDefnTypCd(dqCntlWfDefn); String workflowType = null; if(null!=dqCntlWfDefnTyp){ workflowType = dqCntlWfDefnTyp.getDqCntlWfDefnTypCd(); }
当我运行测试文件时,什么时候不工作,我在buildpath中使用mockito1.8.5 jar.正在模拟服务调用但返回null值.
final DqCntlWfDefnTyp dqCntlWfDefnTyp = controlWfDefTypeService .getDqCntlWfDefnTypCd(dqCntlWfDefn);
该对象dqCntlWfDefnTyp为null
我以前做过这个,并没有问题什么时候,它似乎与我以前做过的文件一起工作.我对测试文件采用了相同的程序,但我无法弄清楚问题.任何人都可以帮助我
感谢所有人提前