2012-06-03 68 views
6

我得到了一個簡單的類與測試用例它和一切正常在日食,但當我運行maven構建(與此目標:安裝cobertura:cobertura checkstyle:checkstyle聚合)在surefire報告中看到驗證錯誤。Maven構建導致與Java 1.7 VerifyError

這裏是確切的錯誤:

------------------------------------------------------------------------------- 
Test set: com.diploma.testProject.ClassUnderTestTest 
------------------------------------------------------------------------------- 
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.034 sec <<< FAILURE! 
testMethodReturnsTrue(com.diploma.testProject.ClassUnderTestTest) Time elapsed: 0.005 sec <<< ERROR! 
java.lang.VerifyError: Expecting a stackmap frame at branch target 79 in method com.diploma.testProject.ClassUnderTest.method(II)Z at offset 24 
    at com.diploma.testProject.ClassUnderTestTest.testMethodReturnsTrue(ClassUnderTestTest.java:37) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103) 
    at $Proxy0.invoke(Unknown Source) 
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145) 
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) 

testMethodReturnFalse(com.diploma.testProject.ClassUnderTestTest) Time elapsed: 0 sec <<< ERROR! 
java.lang.VerifyError: Expecting a stackmap frame at branch target 79 in method com.diploma.testProject.ClassUnderTest.method(II)Z at offset 24 
    at com.diploma.testProject.ClassUnderTestTest.testMethodReturnFalse(ClassUnderTestTest.java:42) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103) 
    at $Proxy0.invoke(Unknown Source) 
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145) 
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69) 

這是類:

public class ClassUnderTest { 
    public static boolean method(int a, int b) { 
     if (a == b) { 
      System.out.println(""); 
      return true; 
     } 
     return false; 
    } 
} 

而且測試:

public class ClassUnderTestTest { 

    @Test 
    public void testMethodReturnsTrue() { 
     assertTrue(ClassUnderTest.method(1, 1)); 
    } 

    @Test 
    public void testMethodReturnFalse() throws Exception { 
     assertFalse(ClassUnderTest.method(1, 2)); 
    } 

} 

而且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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.diploma</groupId> 
    <artifactId>testProject</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>testProject</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.10</version> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 

    <build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
        <testSource>1.7</testSource> 
        <testTarget>1.7</testTarget> 
       </configuration> 
      </plugin> 
     </plugins> 
    </pluginManagement> 
    </build> 
</project> 

將目標設置爲1.5時可以使用,但我需要使用它來使用1.7。 更奇怪的是,修改的方法只有一個這樣的回報:

public class ClassUnderTest { 
    public static boolean method(int a, int b) { 
     if (a == b) { 
      System.out.println(""); 
     } 
     return false; 
    } 
} 

還解決了問題。 我試過在pom屬性塊中設置目標和源版本,但結果是一樣的。

有沒有辦法在1.7版上運行它?

編輯:此外,這個問題只出現在目標cobertura:cobertura。

+0

這似乎是一個常見的問題,與貼在SO非常類似的問題。我發佈瞭解決方案,我們的問題已得到解決:http://stackoverflow.com/a/20001391/1279002 – theINtoy

回答

20

的問題似乎是在的Cobertura。看起來它不支持Java 1.7非常好。 此修復與@ Raghuram爲日食所寫的鏈接類似。 改性POM這樣使得測試工作正常:

<build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.12</version> 
       <configuration> 
        <argLine>-XX:-UseSplitVerifier</argLine> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
        <testSource>1.7</testSource> 
        <testTarget>1.7</testTarget> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
+0

類似的設置適用於buildr:ENV ['JAVA_OPTS'] || =「-XX:UseSplitVerifier」 – Dzhaughn

4

編輯:mvn cobertura:cobertura由於缺乏對Java 7的cobertura支持而無法正常工作。This SO question對此進行了討論。看起來像JaCoCo是要走的路。

mvn install對我maven-3.0.4java 1.7.0_04在Windows 7上正常工作的命令行可能你打this eclipse bug

要添加到@Pau的答案,<pluginManagement>用於多模塊poms中聲明<plugin>元素可以按照兒童的要求繼承。

我得到與pom沒有錯誤的問題,以及下面的代碼片段:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.4</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
       <testSource>1.7</testSource> 
       <testTarget>1.7</testTarget> 
      </configuration> 
     </plugin> 
    </plugins> 

+0

嗯..我也從命令行得到了同樣的錯誤,但現在我發現它從目標cobertura :的Cobertura。運行時只安裝工作正常。 感謝您關於

+0

@ZhivkoDelchev的說明。 'cobertura'得到同樣的錯誤。更新回答 – Raghuram

+0

Cobertura 2.x是新的,聲稱Java 7的支持。 – Dzhaughn