It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published yesterday in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

69 views 0 9
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