android - 使用 robolectric 和 eclipse 运行 android 测试用例时出现 TypeNotPresentException

标签 android eclipse robolectric

我在我正在处理的 android 测试项目中设置了以下示例测试用例。

package com.whatever.test;

import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.xtremelabs.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class SomeClassTest {

  @Test
  public void testFromJson() {
    fail("Not yet implemented");
  }

}

我确实设置了一个 junit 运行配置,它与 junit4 一起运行,我已经尝试过使用 Java 6(默认为 mac os x)和 oracle 的 jdk 7。

无论我怎么尝试,我总是得到以下错误

    java.lang.TypeNotPresentException: Type com.xtremelabs.robolectric.RobolectricTestRunner not present
at sun.reflect.annotation.TypeNotPresentExceptionProxy.generateException(TypeNotPresentExceptionProxy.java:28)
at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:57)
at $Proxy4.value(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
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: com.xtremelabs.robolectric.RobolectricTestRunner
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:95)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:107)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:31)
at sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:370)
at sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:351)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:280)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:222)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3070)
at java.lang.Class.getAnnotation(Class.java:3029)
at org.junit.internal.builders.IgnoredBuilder.runnerForClass(IgnoredBuilder.java:13)
... 12 more

我正在使用 bundle 在 adt 包中的最后一个 android sdk(api 级别 17),我正在使用 robolectric 1.1 作为外部 JAR。

如果以这种方式使用 eclipse 运行测试不是最好的,我愿意接受其他选择。

编辑:有人建议我检查类路径中 Robolectric 是否出现在 Android 之前,我照做了。问题仍然存在。

编辑 2:我已按照评论中的建议进行操作,但似乎没有一个起作用,错误仍然存​​在。

最佳答案

关于android - 使用 robolectric 和 eclipse 运行 android 测试用例时出现 TypeNotPresentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14354962/

相关文章:

java - Xamarin.Android Notification.Notify 不执行任何操作

android - 找不到非具体集合类型的反序列化

java - 构建超过 89 个项目的 JSONArray

java - 如何使用 Selenium 和 Java 手动输入 OTP 后继续自动化

android - 还有其他方法可以为 Android 测试模拟 SharedPreferences 吗?

java - 转换结果时出错 java.io.IOException : Attempted read on closed stream?

eclipse - 有没有办法检索 eclipse 执行的 git 命令?

java - 如何为 SAP Business Objects Java SDK 设置 Eclipse 项目

android - 需要带有 Mockito 的 Robolectric 示例来编写单元测试用例

android - Robolectric TypedArray getString() 返回 null 或空字符串