android - Robolectric 测试在没有消息的情况下抛出 IllegalArgumentException

标签 android unit-testing android-studio jvm robolectric

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)

最佳答案

通过类 AnnotationNodeAnnotationVisitor 我注意到有一个 api 参数传递给 super(api)AnnotationVisitor 应该是 Opcodes#ASM4Opcodes#ASM5Opcodes#ASM6 之一> 或 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"
                }
            }
        }
    }
}

关于android - Robolectric 测试在没有消息的情况下抛出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53987300/

相关文章:

ios - XCTest 与 SenTestingKit 相比有哪些优势?

java - 如何对遗留 J2EE 应用程序进行单元测试

git - 分离的头与分支完全 merge ?

android - 以百分比(或类似百分比)动态调整 View 大小?

java - 日历set()在Android API 23及更低版本上损坏-java.util.Calendar

Android adb shell命令从命令行播放声音

android - 在圆弧的起点和终点画圆

javascript - 在 Dart 中测试性能

android - 类路径中的某些运行时 JAR 文件具有不兼容的版本。考虑从类路径中删除它们

android - 找不到 com.android.databinding :compiler. 3.4.0