Private method coverage when using PowerMock-EasyMock expectPrivate()

The standard way to test a call to a private method in PowerMock is to use createPatialMock on the class being tested, telling PowerMock which method(s) to mock.

In your test, you would code something like:

ClassToTest classToTest = createPartialMock(ClassToTest.class, "getName");

expectPrivate(classToTest, "getName")).andReturn("testName");


When using this PowerMock call on the private method, Clover for Eclipse does not show coverage (green) for the called private method using the expectPrivate option. Is this a bug or is there a setting that I am missing?




1 answer

2 votes



Please let me clarify it a little bit.

Let's say that you have some class with private method, i.e.

public class MyClass {
	private String myPrivate() {
 		return "Hello World!";

And you're having test case with PowerMock usage to test this class i.e.

public void testMethod() throws Exception {
	MyClass myClass = createPartialMock(MyClass.class, "myPrivate");
	expectPrivate(myClass, "myPrivate").andReturn("some string");
	//rest of a test and verification of the test code

If that's the case, then Clover won't show MyClass.myPrivate method as covered because it is not covered at all. This is because of how the mock enhancers (as an PowerMock with EasyMock) are working. These frameworks covers your object with an proxy on which you're operating. That proxy intercepts all method calls and in this case it encounters call for the myPrivate method the value you've set before is being returned. The actual code of that method is never called which results in the not test covered code.


If you're having different problem or I did not clarified it enough, please, feel free to ask.



Grzegorz Lewandowski

Thank you for your answer, Grzegorz. This makes sense; based on the way PowerMock-EasyMock sets up the expectPrivate() call and the fact that we are creating a partial mock of the class under test.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Wednesday in Opsgenie

Getting the Most out of Atlassian and Opsgenie Together

We’re excited to invite you to this action-packed webinar where we will demonstrate how to integrate Opsgenie’s powerful alerting and on-call management tools with your entire Atlassian stack. Mar...

66 views 0 1
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you