2017-01-23 28 views
2

我有一個龐大的Maven多項目構建版本,單元測試主要使用PowerMock,版本爲1.6。 2。我決定整合JaCoCo,所以我們可以開始發佈我們的代碼覆蓋率指標。 JaCoCo的最新版本0.7.8需要PowerMock 1.6.6,所以我升級了我的版本。從PowerMock 1.6.2升級到1.6.6以集成JaCoCo 0.7.8後,「whenNew」調用導致空值

我現在看到一些錯誤的行爲與「whenNew」調用有關,該調用在代碼庫中的所有測試中都會發生變化。

的 「whenNew()」 電話中的問題是這樣的:

PowerMockito.whenNew(BlahBlah.class) 
      .withArguments(any(SomeClass.class)) 
      .thenReturn(blahBlahInstance); 

隨着PowerMock 1.6.2,這是工作的罰款。當我升級到1.6.6時,爲了做到這一點,我必須做出兩個改變,一個是可以接受的,但是很煩人(因爲我不知道爲什麼需要),另一個是不可接受的。

可接受的更改是在班級頂部的「@PrepareForTest」註釋。使用PowerMock 1.6.2,這個列表只用於列出呼叫「new BlahBlah(...)」的類別,而且工作正常。爲了讓這個工作(除了下一個更改),我還必須在該列表中添加「BlahBlah.class」。我可以接受這一點,但如果我對清單上的內容有清晰的說明,這將非常有幫助。

第二個不可接受的更改是我必須將「.withArguments(any(SomeClass.class))」更改爲「.withAnyArguments()」。我也試過「withArguments(anyObject())」,那也失敗了。

更新

請注意,我用的Mockito核心的19年1月10日的版本。我從https://github.com/powermock/powermock/wiki/MockitoUsage#supported-versions看到,PowerMock 1.6.6(1.6.2+)支持該版本的mockito-core。

+0

你有沒有得到解決方案 –

+0

是的。如果您仍有問題,請發佈單獨的問題。 –

回答

0

所有關於你的聲明

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的整合。

+0

如果說實話,在PowerMock 1.6.6之前,添加到'@ PrepareForTest'的所有類由於我們在JaCoCo問題中討論的問題而沒有覆蓋。但是在拋出異常時,它也是'@ SuppressStaticInitializationFor',JaCoCo和TestNG的問題。 –

0

對於我的具體問題,事實證明,這是不工作的唯一地方是當實際的「SomeClass」參數爲空,這是不匹配「any(SomeClass.class)」。在這些情況下,我必須將該條款更改爲「withAnyArguments()」。

相關問題