Mockito Mock static method

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.

<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:

<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:

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.

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

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:

  1. We defined a boolean expection to true.
  2. Another generic String message, to be used as an expectation.
  3. Prepare StringUtil for static method test.
  4. Setting expection when static method checkSubString() is going to get called
  5. Invoking the static method.
  6. Verifying the expected and actual result.

 6. Run the test

When you run above test, you will get below output:
PowerMock test result
That’s all about Mockito mock static method.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.