我正在将 Mockito和PowerMock与JUnit一起使用。使用的覆盖率工具是 ECLEmma 。 使用PowerMock进行了单元测试的代码,覆盖范围显示为红色(未覆盖)。
这是我的POM模拟依赖项
<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>
当我使用
@RunWith(PowerMockRunner.class)
Junit成功运行。但覆盖范围均为红色。什么都不会掩盖
Junit中使用的模拟参考是-
whenNew().withArguments.thenReturn();
ClassName someName = mock(ClassName.class);
WhiteBox.setInternal(classObject, String, classObject2);
Mockito.when().thenReturn();
我尝试关注此帖子 PowerMock ECLEmma coverage issue和 http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/
但是当我添加-
@Rule
public PowerMockRule rule = new PowerMockRule();
static {
PowerMockAgent.initializeIfNeeded();
}
我看到错误
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
关于如何解决此问题的任何建议?
我有这种情况-
SomeClassName mockedObjectOfSomeClassName = mock(SomeClassName.class);
whenNew(SomeClassName.class)
.withArguments("localhost", 8080)
.thenReturn(mockedObjectOfSomeClassName);
为了解决原始问题,可以帮我先将以上声明转换为
doReturn().when();
格式?有人告诉我是否这样做,应该可以解决此问题。