Java Mockito Common Uses (Mock Static, Mock Private…)

In this project, we will be discussing some mockito common uses:

  1. Mock a method
  2. Mock a private method
  3. Mock a static method
  4. Mock a private static method

Before starting, let’s look at the required dependencies and the Class used for test. Also, the whole project can be downloaded here:  https://github.com/nxhoaf/java-code/tree/master/using-mock

Dependencies:

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>${mockito.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>${powermock.version}</version>
      <exclusions>
        <exclusion>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.powermock</groupId>
          <artifactId>powermock-reflect</artifactId>
        </exclusion>
      </exclusions>
      <scope>test</scope>
    </dependency>
  </dependencies>
  

Class to test

public class Validator {
  public int getInteger(String integer) {
    Integer i = new Integer(integer);
    return i;
  }
  public static boolean isValidUrlStatic(String url) {
    boolean isValid = false;
    try {
      new URL(url);
      isValid = true;
    } catch (Exception e) {}
    return isValid;
  }
  public boolean isValidUrl(String url) {
    boolean isValid = false;
    try {
      new URL(url);
      isValid = true;
    } catch (Exception e) {}
    return isValid;
  }
  public static boolean isValidIntegerStatic(String integer) {
    return isValidIntegerPrivateStatic(integer);
  }
  private static boolean isValidIntegerPrivateStatic(String integer) {
    boolean isValid = false;
    try {
      Integer.parseInt(integer);
      isValid = true;
    } catch (Exception e) {}
    return isValid;
  }
  public boolean isValidInteger(String integer) {
    return isValidIntegerPrivate(integer);
  }
  private boolean isValidIntegerPrivate(String integer) {
    boolean isValid = false;
    try {
      Integer.parseInt(integer);
      isValid = true;
    } catch (Exception e) {}
    return isValid;
  }
}

Now, let’s consider the following tests:

1. Mock a method

@RunWith(PowerMockRunner.class)
public class MockTest {
  @Test
  public void isValidUrl() {
    Validator validator = Mockito.mock(Validator.class);
    
    String url = "This is an invalidUrl";
    Mockito.when(validator.isValidUrl(url)).thenReturn(true);
    
    boolean isVaid = validator.isValidUrl(url);
    
    Assert.assertTrue("Should be true", isVaid);
    Mockito.verify(validator, Mockito.timeout(1)).isValidUrl(url);
  }
}

2. Mock a private method

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Validator.class })
public class MockPrivateTest {
  @Test
  public void isValidUrl_WrongUse() throws Exception {
    // False, because we are mocking the whole class, so the real
    // 'isValidInteger' is not
    // getting called
    Validator validator = Mockito.mock(Validator.class);

    String integer = "This is an invali Integer";
    PowerMockito.doReturn(true).when(validator, "isValidIntegerPrivate",
        integer);

    boolean isVaid = validator.isValidInteger(integer);

    Assert.assertTrue("Should be true", isVaid);
    Mockito.verify(validator, Mockito.timeout(1)).isValidUrl(integer);
    PowerMockito.verifyPrivate(validator, Mockito.times(1)).invoke(
        "isValidIntegerPrivate", integer);
  }

  @Test
  public void isValidUrl_CorrectUse() throws Exception {
    // False, because we are mocking the whole class, so the real
    // 'isValidInteger' is not
    // getting called
    // to mock private, PowerMockito must be used. Mockito will not working
    // Validator validator = Mockito.spy(new Validator()); // Not working

    Validator validator = PowerMockito.spy(new Validator());

    String integer = "This is an invali Integer";
    PowerMockito.doReturn(true).when(validator, "isValidIntegerPrivate",
        integer);

    boolean isVaid = validator.isValidInteger(integer);

    Assert.assertTrue("Should be true", isVaid);
    PowerMockito.verifyPrivate(validator, Mockito.times(1)).invoke(
        "isValidIntegerPrivate", integer);
  }
}

3. Mock a static method

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Validator.class })
public class MockStaticTest {
  @Test
  public void testIsValidUrl_First_Solution() {
    PowerMockito.mockStatic(Validator.class);

    String url = "This is not an url";
    PowerMockito.when(Validator.isValidUrlStatic(url)).thenReturn(true);

    boolean result = Validator.isValidUrlStatic(url);
    result = Validator.isValidUrlStatic(url);
    Assert.assertTrue("Should be true", result);

    PowerMockito.verifyStatic(Mockito.times(2));
    Validator.isValidUrlStatic(url);
  }

  @Test
  public void testIsValidUrl_Second_Solution() throws Exception {
    PowerMockito.mockStatic(Validator.class);

    String url = "This is not an url";
    PowerMockito.doReturn(true).when(Validator.class, "isValidUrlStatic",
        url);

    boolean result = Validator.isValidUrlStatic(url);
    Assert.assertTrue("Should be true", result);

    // PowerMockito.verifyStatic(Mockito.times(1));
    PowerMockito.verifyStatic(); // Same as above
    Validator.isValidUrlStatic(url);
  }
}

4. Mock a private static method

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Validator.class })
public class MockPrivateStaticTest {
  @Test
  public void testIsValidInteger_WrongUse() throws Exception {
    // False, because we are mocking the whole class, so the real
    // 'isValidInteger' is not
    // getting called
    PowerMockito.mockStatic(Validator.class);

    String integer = "This is not an url";
    PowerMockito.doReturn(true).when(Validator.class,
        "isValidIntegerPrivateStatic", integer);

    boolean result = Validator.isValidIntegerStatic(integer);
    assertTrue("Should be true", result);
    PowerMockito.verifyPrivate(Validator.class, Mockito.times(1)).invoke(
        "isValidIntegerPrivate", integer);
  }

  @Test
  public void testIsValidInteger_CorrectUse() throws Exception {
    // This is the correct use, we want to mock isValidIntegerPrivate but we
    // still want
    // the real method isValidInteger gets called
    PowerMockito.spy(Validator.class);

    String integer = "This is not an url";
    PowerMockito.doReturn(true).when(Validator.class,
        "isValidIntegerPrivateStatic", integer);

    boolean result = Validator.isValidIntegerStatic(integer);
    Assert.assertTrue("Should be true", result);
    PowerMockito.verifyPrivate(Validator.class, Mockito.times(1)).invoke(
        "isValidIntegerPrivate", integer);
  }
}
Advertisements
This entry was posted in Java, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s