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");

replay(classToTest);

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?

 

Thanks.

 

1 answer

2 votes

Hello,

 

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.

...
@Test
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.

 

Cheers,

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

233 views 0 6
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