我有一个Android项目,我正在尝试使用JUnit,Mockito和Robolectric设置单元测试。我正在使用ADT23(这值得一提,因为我必须手动下载eclipse JDT,因为这个帖子:https://stackoverflow.com/a/25519274/3519951)。所有上述内容都在Windows 7上运行。
运行测试时,我在Android View上遇到NoClassDefFoundError错误。
是的,Android已导入(平台android-15),以及我的Android项目......以下是我的图书馆:
运行配置:
这是完整的堆栈跟踪:
java.lang.NoClassDefFoundError: android/view/View
at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:502)
at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:357)
at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:307)
at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:285)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for android.view.View
at javassist.Loader.findClass(Loader.java:359)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(RobolectricClassLoader.java:82)
at javassist.Loader.loadClass(Loader.java:311)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:59)
... 18 more
Caused by: java.lang.RuntimeException: problem instrumenting public void android.view.View.setContentDescription(java.lang.CharSequence)
at com.xtremelabs.robolectric.bytecode.AndroidTranslator.fixMethod(AndroidTranslator.java:324)
at com.xtremelabs.robolectric.bytecode.AndroidTranslator.fixMethods(AndroidTranslator.java:245)
at com.xtremelabs.robolectric.bytecode.AndroidTranslator.onLoad(AndroidTranslator.java:103)
at javassist.Loader.findClass(Loader.java:340)
... 22 more
Caused by: java.lang.RuntimeException: java.io.IOException: invalid constant type: 15
at javassist.CtClassType.getClassFile2(CtClassType.java:203)
at javassist.CtClassType.subtypeOf(CtClassType.java:303)
at javassist.compiler.MemberResolver.compareSignature(MemberResolver.java:247)
at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:119)
at javassist.compiler.MemberResolver.lookupMethod(MemberResolver.java:96)
at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:704)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:681)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:156)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
at javassist.compiler.TypeChecker.atArrayInit(TypeChecker.java:185)
at javassist.compiler.ast.ArrayInit.accept(ArrayInit.java:28)
at javassist.compiler.TypeChecker.atNewArrayExpr(TypeChecker.java:161)
at javassist.compiler.TypeChecker.atNewExpr(TypeChecker.java:143)
at javassist.compiler.ast.NewExpr.accept(NewExpr.java:72)
at javassist.compiler.JvstTypeChecker.atMethodArgs(JvstTypeChecker.java:220)
at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:702)
at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:681)
at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:156)
at javassist.compiler.ast.CallExpr.accept(CallExpr.java:45)
at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:241)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:329)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:350)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atIfStmnt(CodeGen.java:390)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:354)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.CodeGen.atStmnt(CodeGen.java:350)
at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
at javassist.compiler.Javac.compileStmnt(Javac.java:568)
at javassist.CtBehavior.insertBefore(CtBehavior.java:705)
at javassist.CtBehavior.insertBefore(CtBehavior.java:685)
at com.xtremelabs.robolectric.bytecode.AndroidTranslator.fixMethod(AndroidTranslator.java:321)
... 25 more
Caused by: java.io.IOException: invalid constant type: 15
at javassist.bytecode.ConstPool.readOne(ConstPool.java:1090)
at javassist.bytecode.ConstPool.read(ConstPool.java:1033)
at javassist.bytecode.ConstPool.<init>(ConstPool.java:149)
at javassist.bytecode.ClassFile.read(ClassFile.java:764)
at javassist.bytecode.ClassFile.<init>(ClassFile.java:108)
at javassist.CtClassType.getClassFile2(CtClassType.java:190)
... 57 more
一个大学有一个类似的设置,目前正在运作,但我们找不到任何有意义的差异,除了他运行ADT 22.3。感谢任何帮助,非常感谢!
修改 我想你们中的一些人可能想看一个测试类,这里有一个。你会发现那里甚至没有对Android对象的引用,所以错误与Robolectric的初始化有关......
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.xtremelabs.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class BogusTest
{
@Before
public void setUp() throws Exception
{
}
@Test
public void theTest()
{
assertTrue(true);
}
}
答案 0 :(得分:0)
问题解决了。
我们决定退出使用ADT并使用我们需要的插件安装Eclipse Luna(Android DDMS,Android开发工具,Android原生开发工具)。它并不比安装ADT复杂得多,现在测试运行良好。这个问题仍然无法解释......