-1

I am trying to mock a method using mockito. How to mock if condition using mockito?

Here is my code

@Override public RemedyWrapperOutput createRemedyTicket(RemedyWrapperInput remedyWrapperInput) throws Exception {

    logger.info("Inside createRemedyTicket");

    RemedyWrapperOutput result = new RemedyWrapperOutput();

    final RemedyRecord remData = remedyDao.getByApiAndTitle(remedyWrapperInput.getInstance(),
            remedyWrapperInput.getTitle());

    /**
     * if no records exists in the DB or if the RemedyStatus is
     * CLOSED/RESOLVED/CANCELLED, we create a new ticket.
     */
    if (remData == null ||remData.getStatus().equals(RemedyStatus.RESOLVED)
            || remData.getStatus().equals(RemedyStatus.CLOSED)|| remData.getStatus().equals(RemedyStatus.CANCELLED)) {
        createRemedyTicket(remedyWrapperInput, result);
    } else {
        /* If record exists check if its within duration */

        /**
         * If not within time range create a ticket if New and Assigned
         * status. For all other status stop processing.
         */
        if (!checkIfInTimeRange(remData.getCreationDateTime())) {

            if (remData.getStatus() != null && (remData.getStatus().equals(RemedyStatus.NEW)
                          || remData.getStatus().equals(RemedyStatus.ASSIGNED)
                          || remData.getStatus().equals(RemedyStatus.PENDING)
                          || remData.getStatus().equals(RemedyStatus.IN_PROGRESS))) {

                   int id = remedyDao.create(createRemedyInput(remedyWrapperInput, remData));
                   callRemedyRestApi(remedyWrapperInput, result, id);
                   result.setMessage("Remedy request submitted");
            }  
        } else {
            result.setMessage("A request of this category has already been logged inside specified time range.");
        }

        // Update the last update time to current time
        remedyDao.update(remData);
    }
    return result;
}
Yashaswini H
  • 1
  • 1
  • 1
  • 1

1 Answers1

2

I think you want to look at the method you are trying to mock. Rather than the contents of the method. Let me explain using an example. Here is a simple class that I want to test:

package com.diffblue.javademo;

public class ClassToTest {

  private boolean beenChecked = false;
  private SomeDAO someDAO = new SomeDAO();

  public void checkActive(int id) {
    if (someDAO.isActive(id)) {
      beenChecked = true;
    }
  }

  public boolean getBeenChecked() {
    return beenChecked;
  }
}

It makes a call to SomeDAO which I want to mock:

package com.diffblue.javademo;

public class SomeDAO {

  public  boolean isActive(int id) {
    if (id < 5) {
      return true;
    }
    return false;
  }
}

When I am creating a test case I think about the path through checkActive that I want to test and then pick an answer from isActive that will exercise that path. For example:

package com.diffblue.javademo;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;

import static org.mockito.Mockito.when;

public class ClassToTestTest {

  /**
   * Test the positive side of the if statement in checkActive
   */
  @PrepareForTest({SomeDAO.class})
  @Test
  public void positiveTest() throws Exception {
    // Arrange
    ClassToTest objectUnderTest = new ClassToTest();

    SomeDAO someDAO = PowerMockito.mock(SomeDAO.class);
    when(someDAO.isActive(Matchers.anyInt())).thenReturn(true);

    int activeId = 3;

    // Act
    objectUnderTest.checkActive(activeId);

    // Assert
    Assert.assertTrue(objectUnderTest.getBeenChecked());
  }
}

In this case, I have checked the path where the if statement (line 6 of SomeDAO) is true. So I haven't mocked the if statement, but I have forced a particular code path by carefully selecting the return values that Mockito is giving.

James Wilson
  • 1,541
  • 7
  • 20