所有關於你的聲明
JaCoCo,0.7.8的最新版本首先,需要PowerMock 1.6.6
JaCoCo不需要PowerMock並沒有什麼特殊的JaCoCo PowerMock爲0.7.8。截至今日0.7.8是JaCoCo的最新發布版本。
我猜你的說法是一種誤解是來自閱讀https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo,其中規定
JaCoCo離線儀器儀表只適用於PowerMock版本1.6.6和上部。
根據https://github.com/powermock/powermock/blob/master/changelog.txt
更改日誌1.6。6(2016年11月4日)
- 固定#645 jacoco離線儀器儀表不相容powermock字節碼操作使用@SuppressStaticInitializationFor
當
即有修復只在一個特定的情況下。在其他情況下,早期版本的PowerMock已經使用脫機工具與早期版本的JaCoCo一起工作。
然後根據PowerMock升級到1.6.6後,您的狀態的描述,但是其他變化之前,試圖建立一個Minimal, Complete, and Verifiable example:
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<powermock.version>1.6.6</powermock.version>
<jacoco.version>0.7.8</jacoco.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</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>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src/main/java/org/example/BlahBlah.java
:
package org.example;
public class BlahBlah {
private final SomeClass c;
public BlahBlah(SomeClass c) {
this.c = c;
}
public String run() {
return c.toString();
}
}
src/main/java/org/example/SomeClass.java
:
package org.example;
public class SomeClass {
private final String s;
public SomeClass(String s) {
this.s = s;
}
@Override
public String toString() {
return s;
}
}
src/main/java/org/example/Example.java
:
package org.example;
public class Example {
public String fun() {
return new BlahBlah(new SomeClass("Hello World")).run();
}
}
src/test/java/org/example/ExampleTest.java
:
package org.example;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
@PrepareForTest({Example.class})
@RunWith(PowerMockRunner.class)
public class ExampleTest {
@Test
public void test() throws Exception {
BlahBlah e = new BlahBlah(new SomeClass("Hello PowerMock"));
PowerMockito.whenNew(BlahBlah.class)
.withArguments(any(SomeClass.class))
.thenReturn(e);
assertEquals("Hello PowerMock", new Example().fun());
}
}
和
mvn clean verify
的作品就好了。所以在你的描述中應該有些東西是錯過的,以便真實地再現你的情況。希望這會有所幫助,至少在此基礎上構建更好的再現器。
此外:嘗試解耦JaCoCo與PowerMock升級的整合流程 - 也許這種升級是有問題的,而不是JaCoCo的整合。
你有沒有得到解決方案 –
是的。如果您仍有問題,請發佈單獨的問題。 –