6

我得到了一个带有测试用例的简单类,并且在 Eclipse 中一切正常,但是当我运行 maven build 时(有这个目标:安装 cobertura:cobertura checkstyle:checkstyle-aggregate)我在surefire报告中看到了一个验证错误。

这是确切的错误:

-------------------------------------------------------------------------------
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。

4

2 回答 2

20

问题似乎出在cobertura。看起来它不支持java 1.7 很好。该修复类似于@Raghuram 为 Eclipse 编写的链接。像这样修改 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>
于 2012-06-04T12:33:50.023 回答
4

编辑:mvn cobertura:cobertura由于缺乏对 Java 7 的 cobertura 支持而无法工作。 这个 SO question讨论了这个。看起来JaCoCo是要走的路。

mvn installmaven-3.0.4在 Windows 7和Windows 7 上从命令行对我来说效果很好java 1.7.0_04。可能你遇到了这个 Eclipse 错误

要添加到@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>

于 2012-06-04T04:27:59.163 回答