I am using Mockito and PowerMock with JUnit. The coverage tool used is ECLEmma. The code that is unit tested with PowerMock, the coverage is shown in red (uncovered).
Here are my the POM mock dependencies
<powermock.version>1.6.5</powermock.version>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-javaagent</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
When I'm using
@RunWith(PowerMockRunner.class)
The Junit runs successfully. but the coverage is all RED. Nothing gets covered
The Mocking references used in the Junit are -
whenNew().withArguments.thenReturn();
ClassName someName = mock(ClassName.class);
WhiteBox.setInternal(classObject, String, classObject2);
Mockito.when().thenReturn();
I tried following this post PowerMock ECLEmma coverage issue and http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/
But when I add -
@Rule
public PowerMockRule rule = new PowerMockRule();
static {
PowerMockAgent.initializeIfNeeded();
}
I see the error
java.lang.NoClassDefFoundError: org/powermock/classloading/ClassloaderExecutor
at org.powermock.modules.junit4.rule.PowerMockRule.apply(PowerMockRule.java:44)
at org.junit.runners.BlockJUnit4ClassRunner.withMethodRules(BlockJUnit4ClassRunner.java:365)
at org.junit.runners.BlockJUnit4ClassRunner.withRules(BlockJUnit4ClassRunner.java:355)
at
Caused by: java.lang.ClassNotFoundException: org.powermock.classloading.ClassloaderExecutor
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more
Any suggestions on how this can be resolved?
I have this situation -
SomeClassName mockedObjectOfSomeClassName = mock(SomeClassName.class);
whenNew(SomeClassName.class)
.withArguments("localhost", 8080)
.thenReturn(mockedObjectOfSomeClassName);
In order to solve the original issue, can some help me first convert this above statement into
doReturn().when();
format? I was told if we do that, that should fix the issue.