EasyMock Static Method – PowerMock, JUnit 4, TestNG

Filed Under: EasyMock

One of the limitations of EasyMock is that it can’t mock static methods. However, we can use PowerMock EasyMock extension to mock static methods.

EasyMock Static Method – PowerMock

PowerMock is divided into multiple modules to support JUnit and TestNG testing framework. Similarly, there are modules to extend EasyMock and Mockito mocking frameworks.

I will provide an example to mock static method using PowerMock on both JUnit 4 and TestNG frameworks. So we need to import the following artifacts.

  • powermock-module-junit4
  • powermock-module-testng
  • powermock-api-easymock
  • junit, testng and easymock for obvious reasons.

I am not using JUnit 5 because PowerMock doesn’t support it yet. I am using the following versions for my examples.


<junit4.version>4.12</junit4.version>
<testng.version>6.14.3</testng.version>
<powermock.version>2.0.0-beta.5</powermock.version>
<java.version>10</java.version>
<easymock.version>3.6</easymock.version>

JUnit PowerMock EasyMock Static Method Example

  • First step is to annotate test class with @RunWith(PowerMockRunner.class) annotation.
  • Next step is to specify the classes to prepare for testing using PowerMock, for example @PrepareForTest(Utils.class). This has to be done at the class level and we can use its fullyQualifiedNames to specify multiple classes and packages.
  • In the test method, use PowerMock.mockStatic() method to mock the static methods of the class.
  • Stub the behaviors using EasyMock.expect() method.
  • Since we don’t have a mock object, use PowerMock.replayAll() to finalize the setup.
  • Use asserts to test the behaviors.
  • Use PowerMock.verifyAll() to verify that all the stubbed methods were called.

Let’s say we have a utility class as:


class Utils {

	public static long generateID() {
		return System.currentTimeMillis();
	}
}

Here is the JUnit test to mock the static method and test it.


package com.journaldev.easymock.powermock.staticmethod;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.easymock.PowerMock.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Utils.class)
public class JUnit4PowerMockEasyMockStaticExample {

	@Test
	public void test_static_method() {
		//PowerMock.mockStatic()
		mockStatic(Utils.class);
		
		expect(Utils.generateID()).andReturn(1000L);
		
		//PowerMock.replayAll()
		replayAll();
		
		assertEquals(1000L, Utils.generateID());
		//PowerMock.verifyAll()
		verifyAll();
	}
}

TestNG PowerMock EasyMock Static Method Example

If you want to use TestNG instead of JUnit-4, then make sure your test class extends PowerMockTestCase class. Also remove the @RunWith annotation. Make necessary changes to other annotations and assert methods.

Below class uses TestNG along with PowerMock to mock static methods using EasyMock.


package com.journaldev.easymock.powermock.staticmethod;

import static org.easymock.EasyMock.*;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.Test;

import static org.powermock.api.easymock.PowerMock.*;
import static org.testng.Assert.assertEquals;

@PrepareForTest(Utils1.class)
public class TestNGPowerMockEasyMockStaticExample extends PowerMockTestCase{

	@Test
	public void test_static_method() {
		//PowerMock.mockStatic()
		mockStatic(Utils1.class);
		
		expect(Utils1.generateID()).andReturn(1000L);
		
		//PowerMock.replayAll()
		replayAll();
		
		assertEquals(1000L, Utils1.generateID());
		//PowerMock.verifyAll()
		verifyAll();
	}
}
class Utils1 {

	public static long generateID() {
		return System.currentTimeMillis();
	}
}

Summary

PowerMock is a great extension to EasyMock and Mockito mocking frameworks. It helps us by extending our test cases to mock static methods too.

You can checkout complete project and more EasyMock examples from our GitHub Repository.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages