How to Use Mockito to Mock Static Method in Java

Sheeraz Gul Feb 02, 2024
How to Use Mockito to Mock Static Method in Java

This tutorial demonstrates how we can use the mockito to mock the static method in Java.

Mock Static Method in Java

Mocking Static Methods are required when performing unit testing in Java. Before the Mockito 3.4.0 version, we need to use PowerMock to mock the static and private methods.

With Mockito 3.4.0, we can directly mock the static methods. This tutorial demonstrates how to mock static methods using mockito in Java.

Add Maven Dependency

Before starting with mockito, we need to add it to our system. To mock the static method, we require inline mock-making from mockito inline mocking.

The mockito-inline is developed separately for community feedback which provides the mocking facility for static methods, and the final class, which were unmockable previously; here is the maven dependency for mockito inline mocking:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>4.6.1</version>
    <scope>test</scope>
</dependency>

Add this dependency to pom.xml of your maven system, and you are good to go.

Different Code Examples Using Mockito Inline Mocking

Let’s first decide on a system we can apply to mock static methods. Let’s create a simple class with two methods, one with arguments and one without arguments. See Example:

package delftstack;

import java.util.stream.IntStream;

class Mock_Static_Method {
  public static String get_Value() {
    return "delftstack";
  }

  public static int addition(int... args) {
    return IntStream.of(args).sum();
  }
}

The code above contains a simple class that has two methods:

  1. The first method returns the value delftstack and doesn’t take any argument.
  2. The second method takes arguments and returns their sum.

Mock Static Class

Before mocking the static method, we need to mock the static class. Here is the code example of how to mock a static class using mockito in Java:

try (MockedStatic Mock_Static_Class = mockStatic(AppStatic.class)) {
  // Here, we can record mock expectations, test code, and verify mock
}

The MockedStatic in the above code represents the scoped and active mocks of type static methods. The mockStatic is used to verify static method invocations.

Mock Static Method with No Arguments

Let’s try to mock the first method, get_Value(), which takes no arguments and returns the value delftstack. We will try to mock the get_Value() method and return the value delftstack1; see the example:

@Test
public void Testing_Get_Value() {
  // Outside the scope
  assertEquals("delftstack", Mock_Static_Method.get_Value());

  try (MockedStatic Mock_Static = mockStatic(Mock_Static_Method.class)) {
    Mock_Static.when(Mock_Static_Method::get_Value).thenReturn("delftstack1");

    // Inside the scope
    assertEquals("delftstack1", Mock_Static_Method.get_Value());
    Mock_Static.verify(Mock_Static_Method::get_Value);
  }

  // Outside the scope
  assertEquals("delftstack", Mock_Static_Method.get_Value());
}

The code above mocks the static method get_Value(), and now, outside the scope, the method will return delftstack, and inside the scope, it will return delftstack1.

Mock Static Method with Arguments

There is not much difference between mocking a static method with arguments and a static method without arguments.

To mock static method with arguments, we can use flexible argument-matches in expectations and lambda expression to invoke the methods; see example:

@Test
public void Testing_Addition() {
  assertEquals(3, Mock_Static_Method.addition(1, 2));

  try (MockedStatic Mock_Static = mockStatic(Mock_Static_Method.class)) {
    Mock_Static.when(() -> Mock_Static_Method.addition(anyInt(), anyInt())).thenReturn(10);

    assertEquals(10, Mock_Static_Method.addition(1, 2));
    Mock_Static.verify(() -> ClassWithStaticMethod.addition(1, 2));
  }

  assertEquals(3, Mock_Static_Method.addition(1, 2));
}

The code above mocks the static method with arguments from the code above.

The code above will return the sum of given arguments as it is mocking the IntStream.of(args).sum(), which is used to calculate the sum of given arguments. Outside the scope, the method will return the sum 3, and inside the scope, it should return the value 10.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook