Mocking Static Methods in JUNIT5

PowerMock is a Java framework that allows you to unit test code that uses static methods, constructors, and private methods. PowerMock uses a combination of bytecode manipulation and reflection to enable the mocking of these types of methods.

PowerMock is not officially supported by JUnit 5. PowerMock is built on top of EasyMock and provides additional functionality such as the ability to mock static methods, constructors, and private methods. PowerMock uses a different test runner than the one provided by JUnit 5, so it is not directly compatible with JUnit 5.

Alternatively, you can use mockito-inline for inline mocking, it allows you to mock static methods, final classes, and methods, and more.

JUnit 5 provides its own extension model that allows for the creation of custom test runners and the registration of extensions. You could write your own test runner for PowerMock that works with JUnit 5 or use a different mocking framework that is compatible with JUnit 5.

PowerMock is not officially supported by JUnit 5
If you are using JUnit5 and you really need to use Powermock, you could use the powermock-module-junit5 and powermock-api-mockito2 which have support for JUnit5, but it’s not recommended as it’s not officially supported by JUnit5, and to use mockito-inline as suggested.

JUNIT5 Mock Static Method Code Examples

Here’s an example of how to use mockito-inline to mock a static method in JUnit 5. Starting with a Utils class, we displaying an example of us initializing a Utils class with a static method of getString().

Utils.java class
This is a simple utils class used by JAVA, where it has a static method of getString().

1
2
3
4
5
public class Utils {
    public static String getString() {
        return "Original string";
    }
}

MyTest.java class
This is an example of a class utilizing the utils class. As you can see in the code, line:10 && line:11 is performing the mocking mechanism. It is important to note that we are using mockito-inine which introduced Mockito.mockStatic, where we can mock the static method for the Utils class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.mockito.Mockito;

public class MyTest {

    @Test
    public void testStaticMethod() {
        // mock the static method of the Utils class
        Mockito.mockStatic(Utils.class);
        Mockito.when(Utils.getString()).thenReturn("Mocked string");
       
        // create an instance of the class that calls the static method
        MyClass myClass = new MyClass();
        assertEquals("Mocked string", myClass.getStringFromUtils());
    }
}

pom.xml
These are the maven dependencies being used to get the code working as expected for the JUNIT5 examples above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.11.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.11.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>3.6.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>3.6.0</version>
    <scope>test</scope>
</dependency>

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

One thought on “Mocking Static Methods in JUNIT5

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top