1

I am using Mockito in my JUnit test cases. I have a Java application, the relevant part of the method is:

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);

This is in the method OpenClosedFaultsStatusRequestFunction.getFasoDetailsForFASOID()

My JUnit method where I am mocking looks like this:

@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);

    }

My idea is to mock the dao call from method in the service clacc, but when I am executing my test case, it is giving NullPointerException in the line openClosedFaultsStatusRequestFunction.getFasoDetailsForFASOID(anyString); because the dao call from my service class is not getting mocked. Can anyone please suggest the right approach?

Anirban
  • 925
  • 4
  • 24
  • 54
  • I don't remember the method names but you need to do something like "Mockito.do().when()" then it will not call the original method – ave4496 Dec 24 '18 at 16:02
  • Possible duplicate of [Mockito: Trying to spy on method is calling the original method](https://stackoverflow.com/questions/11620103/mockito-trying-to-spy-on-method-is-calling-the-original-method) – ave4496 Dec 24 '18 at 16:04
  • It is not working either using this method or using spy – Anirban Dec 25 '18 at 04:45
  • 1
    Where does `openClosedFaultsStatusRequestFunction` come from in your test method? – ave4496 Dec 25 '18 at 13:00

0 Answers0