无法使用Mockito模拟方法调用

时间:2018-12-24 13:48:29

标签: mockito junit4 java-7

我在JUnit测试用例中使用Mockito。我有一个Java应用程序,该方法的相关部分是:

if (requestType.equalsIgnoreCase("Open") || requestType.trim().equals("")) {
                    if (daysBack != null) {
                        openClosedFaultStatusRequestData = openClosedFaultStatusRequestDao.getFmOvOnOpenFasoId(value);
                        if (openClosedFaultStatusRequestData == null) {
                            isOpenFasoPresent = false;
                            logger.info("isOpenFasoPresent false for open faso");
                        } else {
                            isOpenFasoPresent = true;
                            logger.info("openClosedFaultStatusRequestData: " + openClosedFaultStatusRequestData.getFasoId());
                            fasxml.appendFasoOpenClosedFault(openClosedFaultStatusRequestData);
                            fasxml.appendNoOfTT(recordCnt);
                            fasxml.setStatus(fasXML.SUCCESS_REPLY);
                            tmptime = new fasDate(new java.util.Date());
                            endTime = tmptime.format(fasDate.FAS_DATETIME_FORMATTER);
                            fasXMLreply = fasxml.makereplyXML(DataConstants.OPEN_CLOSED_FASO_STATUS_REQ);

这在方法OpenClosedFaultsStatusRequestFunction.getFasoDetailsForFASOID()

我正在模拟的JUnit方法如下:

@Test
    public void getFasoDetailsByFasoId() {
        OpenClosedFaultStatusRequestDAO openClosedFaultStatusRequestDao = Mockito.mock(OpenClosedFaultStatusRequestDAO.class);
        Mockito.when(openClosedFaultStatusRequestDao.getFmOvOnOpenFasoId(Mockito.anyString())).thenReturn(openClosedFaultStatusRequestData);

        replyXml = openClosedFaultsStatusRequestFunction.getFasoDetailsForFASOID(anyString);
        System.out.println(replyXml);

    }

我的想法是模拟来自服务clacc中方法的dao调用,但是当我执行测试用例时,它将在openClosedFaultsStatusRequestFunction.getFasoDetailsForFASOID(anyString);行中提供NullPointerException;因为我的服务类中的dao调用没有被嘲笑。有人可以建议正确的方法吗?

0 个答案:

没有答案