首页 > 解决方案 > Robolectric 测试抛出 IllegalArgumentException 而没有消息

问题描述

RobolectricTestRunner::class在我的单元测试中IllegalArgumentException,这个堆栈跟踪没有任何消息:

我的单元测试类:

@RunWith(RobolectricTestRunner::class)
class UnitTest {
    @Test
    fun testEncodeDecode() {
    }
}

输出堆栈跟踪:

java.lang.IllegalArgumentException
at org.objectweb.asm.AnnotationVisitor.<init>(Unknown Source)
at org.objectweb.asm.AnnotationVisitor.<init>(Unknown Source)
at org.objectweb.asm.tree.AnnotationNode.<init>(AnnotationNode.java:77)
at org.objectweb.asm.tree.AnnotationNode.<init>(AnnotationNode.java:63)
at org.objectweb.asm.tree.ClassNode.visitAnnotation(ClassNode.java:208)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.robolectric.internal.bytecode.ClassInstrumentor.analyzeClass(ClassInstrumentor.java:63)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$maybeInstrumentClass$1(SandboxClassLoader.java:121)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:120)
at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass$0(SandboxClassLoader.java:111)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:110)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.robolectric.RobolectricTestRunner.getHooksInterface(RobolectricTestRunner.java:551)
at org.robolectric.RobolectricTestRunner.configureSandbox(RobolectricTestRunner.java:235)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:230)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

标签: androidunit-testingandroid-studiojvmrobolectric

解决方案


通过这些类AnnotationNodeAnnotationVisitor我注意到api传递给的参数super(api)应该AnnotationVisitorOpcodes#ASM4or Opcodes#ASM5or Opcodes#ASM6or之一,Opcodes#ASM7但构造函数只接受前两个。

我注意到有org.ow2.asm:asm两个不同版本的两个依赖项org.ow2.asm:asm:7.0org.ow2.asm:asm:5.0.4这是问题的根源。

要解决此问题,您必须强制 Gradle 仅导入 7.0 版本,如下所示:

android {
    allprojects {
        configurations {
            all {
                resolutionStrategy {
                     force "org.ow2.asm:asm:7.0"
                }
            }
        }
    }
}

推荐阅读