首页 > 解决方案 > java.lang.VerifyError 的 JUnit 错误:Jenkins 上的局部变量类型错误

问题描述

我正在用 maven 项目测试 Junit 的功能

这是我正在测试的功能

public boolean isJSONValid3(String jsonInString) {
        final Gson gson = new Gson();
        try {
            gson.fromJson(jsonInString, Object.class);
            return true;
        } catch(JsonSyntaxException ex) {
            return false;
        }
    }

以及 Jenkins 上的错误跟踪

junit.framework.AssertionFailedError: Exception in constructor: testIsJSONValid3 (java.lang.VerifyError: Bad local variable type
Exception Details:
  Location:
    com/egroup/util/AttributeCheck.objectNotNull([Ljava/lang/Object;)Z @29: iload
  Reason:
    Type top (current frame, locals[5]) is not assignable to integer
  Current Frame:
    bci: @29
    flags: { }
    locals: { 'com/egroup/util/AttributeCheck', '[Ljava/lang/Object;', integer, integer }
    stack: { }
  Bytecode:
    0x0000000: 033d 11ff ff3e 120b 1100 26b8 0011 2bbe
    0x0000010: 3604 120b 1100 27b8 0011 0336 0515 0515
    0x0000020: 0411 0027 3d11 0000 3ea2 0076 1c11 0027
    0x0000030: a000 161d 1100 00a0 000f 120b 1c1d 04b8
    0x0000040: 0031 11ff ff3e 120b 1100 28b8 0011 2b15
    0x0000050: 0532 1100 283d 1100 003e c700 271c 1100
    0x0000060: 28a0 0016 1d11 0000 a000 0f12 0b1c 1d04
    0x0000070: b800 3111 ffff 3e12 0b11 0029 b800 1103
    0x0000080: ac1d 9b00 0f12 0b1c 1d03 b800 3111 ffff
    0x0000090: 3e12 0b11 0027 b800 1184 0501 a7ff 811d
    0x00000a0: 9b00 0f12 0b1c 1d03 b800 3111 ffff 3e12
    0x00000b0: 0b11 002c b800 1104 ac                 
  Stackmap Table:
    append_frame(@29,Integer,Integer)
    same_frame_extended(@153)
    chop_frame(@183,1)

    at com.egroup.util.AttributeCheckTest.<init>(AttributeCheckTest.java:13)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at junit.framework.TestSuite.createTest(TestSuite.java:65)
    at junit.framework.TestSuite.addTestMethod(TestSuite.java:307)
    at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
    at junit.framework.TestSuite.<init>(TestSuite.java:129)
    at com.egroup.util.AttributeCheckTest.suite(AttributeCheckTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.SuiteMethod.testFromSuiteMethod(SuiteMethod.java:35)
    at org.junit.internal.runners.SuiteMethod.<init>(SuiteMethod.java:24)
    at org.junit.internal.builders.SuiteMethodBuilder.runnerForClass(SuiteMethodBuilder.java:11)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
)
    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.TestCase.fail(TestCase.java:227)
    at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

在eclipse ide上运行junit时可以工作,但是

错误时:

使用 Publish JUnit 报告和 Maven 安装在 Jenkins 上运行

jdk:java-1.8.0-openjdk-1.8.0.222-4

如何解决这个问题!?!?

这让我烦了几天!

标签: javajenkinsjunitjava-8

解决方案


我修复了我的 pom.xml,在属性和插件中添加 -noverify 以通过字节码验证

<properties>
    <argLine>-noverify</argLine>
</properties>

和插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version> <!-- or whatever current version -->
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <argLine>-noverify</argLine>
        </configuration>
</plugin>

它有效!顺便问一下,有没有更好的解决方案?


推荐阅读