Table of Contents
In this post, we will see about Mockito Mock static method.
If you want to mock static methods, you need to use PowerMockito.PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.
Let’s create a simple example to mock static method
using powermockito.
1. Create a simple java maven project.
2. Adding Dependencies with Maven
Let’s add PowerMock dependencies to our maven project.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.4</version> <scope>test</scope> </dependency> |
Your pom.xml will look like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.arpit.java2blog</groupId> <artifactId>PowerMockitoExample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.4</version> <scope>test</scope> </dependency> </dependencies> </project> |
Now that we are done adding dependencies, let’s enable the use of annotations in our tests.
3. Enabling PowerMock Annotations
Just like what we need to do with Mockito, we also need to enable the use of Annotations with PowerMockito. Much like Mockito, we make use of similar annotations, as shown:
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(fullyQualifiedNames = "org.arpit.java2blog.*") public class PowerTest { ... } |
Let us look at each annotation we used above:
@RunWith
annotation is similar to what we did in Mockito. Instead of using Mockito, we will use a PowerMockRunner this time, which will enable PowerMockito APIs in the test.@PrepareForTest
 informs PowerMockito which classes to prepare with Java Reflection API for testing.
 4. Create class with static method
create a class named StringUtil
. This is the class under test. It has one static method named checkSubString()
. We are going to mock this checkSubString()
 using PowerMockito.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog.mockito; public class StringUtil { public static boolean checkSubString(String s1,String s2) { if(s1.contains(s2)) { return true; } else { return false; } } } |
 5. Create test class to mock static method
Create class named PowerMockStaticMethodTest
to mock static method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.arpit.java2blog.mockito; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(StringUtil.class) public class PowerMockStaticMethodTest { @Test public void test_StringUtil_staticMethod() { boolean expectation=true; // 1 PowerMockito.mockStatic(StringUtil.class); //2 PowerMockito.when(StringUtil.checkSubString("Java2Blog","blog")).thenReturn(true); //3 boolean actual = StringUtil.checkSubString("Java2Blog","blog"); //4 Assert.assertEquals(expectation, actual); //5 } } |
Let go through each step one by one:
- We defined a boolean expection to
true
. - Another generic String message, to be used as an expectation.
- PrepareÂ
StringUtil
for static method test. - Setting expection when static method
checkSubString()
is going to get called - Invoking the static method.
- Verifying the expected and actual result.
 6. Run the test
When you run above test, you will get below output:
That’s all about Mockito mock static method.