Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Private method coverage when using PowerMock-EasyMock expectPrivate()

Robert Jacobsen August 18, 2015

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
Grzegorz Lewandowski
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 18, 2015

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

Robert Jacobsen August 19, 2015

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
TAGS
AUG Leaders

Atlassian Community Events