android - 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)
解决方案
通过这些类AnnotationNode
,AnnotationVisitor
我注意到api
传递给的参数super(api)
应该AnnotationVisitor
是Opcodes#ASM4
or Opcodes#ASM5
or Opcodes#ASM6
or之一,Opcodes#ASM7
但构造函数只接受前两个。
我注意到有org.ow2.asm:asm
两个不同版本的两个依赖项org.ow2.asm:asm:7.0
,org.ow2.asm:asm:5.0.4
这是问题的根源。
要解决此问题,您必须强制 Gradle 仅导入 7.0 版本,如下所示:
android {
allprojects {
configurations {
all {
resolutionStrategy {
force "org.ow2.asm:asm:7.0"
}
}
}
}
}
推荐阅读
- visual-studio-2019 - 如何在 vs2019 中正确重命名 .razor 文件?
- spring - Spring security:输入正确的凭据后无法访问目标页面
- jwt - Docusign API 的 JWT 问题
- c - 为什么在下面的c语句中,num值可以改变
- python - 使用另一个数据框列表过滤数据框列表
- javascript - React Native:如果给定值已经存在,是否有一种简单的方法来更新所有对象值?
- python - 使用经过训练的 Keras 模型对新的 csv 数据进行预测
- python - 如何比较两次
- java - 如何在java + SpringBoot中生成带有转义字符“\ n”的JSON
- java - 在另一个 java method1 中调用 java method2 并且不要等待 method2 响应并继续执行