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 Join to answer
Community showcase
Emilee Spencer
Published Friday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

66 views 0 3
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot