我的 Android 工作区中有一个类库。我现在尝试使用 JUnit 和 UNIT 测试来测试每个类。
我为库中的纯 java 类编写的第一个测试,它运行顺利。
我为一个使用一些 Android 类 (Rect
) 的类编写的第二个测试,测试运行,但失败, classNotFoundException
当它必须实例化目标类中的 Rect
字段时。
以下是有问题的代码(第 1161 行):
1156 public Symbol(String name, int id) {
1157 validateName(name);
1158 this.name = name;
1159 this.id = id;
1160 this.value = null;
1161 this.location = new Rect();
1162 this.precedence = 0;
1163 this.associativity = Associativity.PRECEDENCE;
1164 }
这是错误消息:
java.lang.NoClassDefFoundError: android/graphics/Rect
at com.Common.lib.Grammar$Symbol.<init>(Grammar.java:1161)
at com.Common.lib.Grammar$Terminal.<init>(Grammar.java:1285)
at com.Common.lib.Grammar$Terminal.<init>(Grammar.java:1284)
at com.Common.lib.Grammar.createTerminal(Grammar.java:558)
at com.Common.lib.Grammar.<init>(Grammar.java:77)
at com.Common.lib.GrammarTest.testGrammar(GrammarTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
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: android.graphics.Rect
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 30 more
最佳答案
这是因为您的测试类路径不包含 android 类。来自文档(https://developer.android.com/tools/testing/testing_android.html):
Android test suites are based on JUnit. You can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components. If you're new to Android testing, you can start with general-purpose test case classes such as AndroidTestCase and then go on to use more sophisticated classes.
如果您的代码是 apklib(而不是应用程序),则有两种测试方法。引用文档)
- You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features.
- You can set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test.
http://developer.android.com/tools/projects/index.html#testing
作为奖励(不是非强制),您可以使用 roboelectric ( http://robolectric.org/ ) 来避免 java.lang.RuntimeException: Stub
(但不是 ClassNotFoundException
)异常并在没有模拟器的情况下运行测试。
关于java - 使用 JUnit 测试库项目无法实例化 android 类元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21984760/