EasyMock Private Method Mock using PowerMock

Filed Under: EasyMock

Sometimes we want to test a method that is using a private method. We can create the mock object using EasyMock but EasyMock doesn’t allow us to mock private methods. So we can use PowerMock EasyMock API extension to mock a class private methods.

EasyMock Private Method Mock using PowerMock

For stubbing private method behavior, we have to use PowerMock.createPartialMock() to get the mock object. This is required so that we don’t mock the methods where these private methods are being called.

After that, use PowerMock.expectPrivate() method to stub the private method behavior.

Make sure to call PowerMock.replay() before writing the test code that uses the stubbed methods. For stub methods call verification, use PowerMock.verify() method.

EasyMock Private Method – JUnit 4

PowerMock doesn’t support JUnit 5 as of now, so I will use JUnit 4 for writing test cases.

Let’s say we have a class defined as:


class Foo {
	private boolean isInit() {
		return false;
	}
	private String reverse(String s) {
		return null;
	}
	public boolean checkStatus() {
		return isInit();
	}
	public String doReverse(String s) {
		return reverse(s);
	}
}

Notice that the public methods are internally calling the class private methods.

Here is the test class where we are mocking the private method behaviors.


package com.journaldev.easymock.powermock.privatemethod;

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.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.easymock.PowerMock.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Foo.class)
public class JUnit4PowerMockEasyMockPrivateExample {

	@Test
	public void test_private_method() throws Exception {
		Foo mock = createPartialMock(Foo.class, "isInit", "reverse");
		
		expectPrivate(mock, "isInit").andReturn(true);
		expectPrivate(mock, "reverse", "cat").andReturn("tac");
		expectPrivate(mock, "reverse", "123").andReturn("321");
		replay(mock);
		
		assertTrue(mock.checkStatus());
		assertEquals("tac", mock.doReverse("cat"));
		assertEquals("321", mock.doReverse("123"));
		
		verify(mock);
	}
}

Notice that we are creating partial mock for only private methods, public methods are not mocked. So when we call these public methods, the real method will get called that will internally call the private method and our stubbed method implementation will be used.

EasyMock Private Method – TestNG

Here is the example of mocking private method written in TestNG testing framework.


package com.journaldev.easymock.powermock.privatemethod;

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;
import static org.testng.Assert.assertTrue;

@PrepareForTest(Foo.class)
public class TestNGPowerMockEasyMockPrivateExample extends PowerMockTestCase{

	@Test
	public void test_private_method() throws Exception {
		Foo mock = createPartialMock(Foo.class, "isInit", "reverse");
		
		expectPrivate(mock, "isInit").andReturn(true);
		expectPrivate(mock, "reverse", "cat").andReturn("tac");
		expectPrivate(mock, "reverse", "123").andReturn("321");
		replay(mock);
		
		assertTrue(mock.checkStatus());
		assertEquals("tac", mock.doReverse("cat"));
		assertEquals("321", mock.doReverse("123"));
		
		verify(mock);
	}
}

Notice that the TestNG class is extending PowerMockTestCase class, so that PowerMock API is used to run the test cases.

Summary

PowerMock is a great extension API for EasyMock and Mockito mocking frameworks. It’s easily integrated with EasyMock and JUnit/TestNG frameworks. I hope they start supporting JUnit 5 real soon.

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