Robolectric启动时反射的NullPointerException - 任何提示?

时间:2010-11-21 05:16:41

标签: java android junit nullpointerexception

我正在Eclipse上开发一个Android项目,我正试图从模拟器/设备上的运行测试(非常慢)切换到Robolectric

我用Robolectric取代了Android libs,将JUnit添加到路径中,将测试用例更改回常规TestCase并添加了来自Quick Start guide的建议@RunWith(RobolectricTestRunner.class)(需要更改以实例化我的Activity而不是依赖Android的活动测试为我做这个。)

但是,当我运行测试时,我得到:

java.lang.NullPointerException
 at com.xtremelabs.robolectric.RobolectricTestRunner.isInstrumented(RobolectricTestRunner.java:123)
 at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:72)
 at com.xtremelabs.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:57)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
 at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
 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:29)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
 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)

这似乎与RobolectricTestRunner.java中的代码(from 0.9 tag,因为0.9.1标记没有它)有关:

private static boolean isInstrumented() {
    return RobolectricTestRunner.class.getClassLoader().getClass().getName().contains(RobolectricClassLoader.class.getName());
}

但我无法理解,因为这种反思应该来自班级本身。

另外,我可以发誓它在重复失败之前运行了一次 - 我删除了robolectric缓存(如建议的here)并用最新的(4.8.2)替换了Eclipse的JUnit,但没有任何改变。 / p>

任何提示?

更新:我尝试创建一个新的Java(即简单的非Android)测试项目,就像快速入门指南中所述。但是,现在测试运行抱怨没有AndroidManifest.xml:

java.lang.RuntimeException: java.io.FileNotFoundException: /Users/chester/Documents/workspace/minitruco-android/minitruco-android-robolectric-test/AndroidManifest.xml (No such file or directory)
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:245)
    at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:215)
    at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:163)
    at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:143)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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.io.FileNotFoundException: /Users/chester/Documents/workspace/minitruco-android/minitruco-android-robolectric-test/AndroidManifest.xml (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:137)
    at java.io.FileInputStream.<init>(FileInputStream.java:96)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:87)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:178)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at com.xtremelabs.robolectric.RobolectricTestRunner.findResourcePackageName(RobolectricTestRunner.java:255)
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:236)
    ... 18 more

我想测试应该从原始项目(minitruco-android)加载文件,这些文件出现在测试项目的构建路径和Eclipse上的Run / Debug构建配置中(在测试项目的类路径条目下)。无论如何,我已经从我原来的测试项目中复制了文件(仅仅是为了测试),但后来却抱怨R.java(和 应该从原始项目中导入) :

java.lang.RuntimeException: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for me.chester.minitruco.test.R
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:245)
    at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:215)
    at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:163)
    at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:143)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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 me.chester.minitruco.test.R
    at javassist.Loader.findClass(Loader.java:359)
    at com.xtremelabs.robolectric.RobolectricClassLoader.findClass(RobolectricClassLoader.java:60)
    at javassist.Loader.loadClass(Loader.java:311)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at com.xtremelabs.robolectric.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:37)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at com.xtremelabs.robolectric.RobolectricTestRunner.createResourceLoader(RobolectricTestRunner.java:237)
    ... 18 more
Caused by: javassist.NotFoundException: me.chester.minitruco.test.R
    at javassist.ClassPool.get(ClassPool.java:436)
    at com.xtremelabs.robolectric.AndroidTranslator.onLoad(AndroidTranslator.java:68)
    at javassist.Loader.findClass(Loader.java:340)
    ... 26 more

我的设置和快速启动之间唯一不同的是我在测试项目上保留测试源并在常规项目上保留常规源(自第一次构建以来,我认为测试项目看到常规源 - 当然,这在很大程度上取决于)。

我正在使用SoyLatte,但也尝试了Mac OS的默认1.6 JVM,结果相同。

谢谢。

3 个答案:

答案 0 :(得分:9)

过去我们遇到过同样的问题。对我们有用的是扩展 RoboletricTestRunner 并为其提供一个了解我们项目位置的 RoboletricConfig 实例。这样我就不必修改任何带有eclipse的运行配置,它只是起作用。

    package ...;

    import java.io.File;

    import org.junit.runners.model.InitializationError;

    import com.xtremelabs.robolectric.RobolectricConfig;
    import com.xtremelabs.robolectric.RobolectricTestRunner;

    public class TestRunner extends RobolectricTestRunner {

        public static final string MAIN_PROJECT_PATH = "../path_to_android_project";

        public TestRunner(Class<?> testClass) throws InitializationError {
             super(testClass, new RobolectricConfig(new File(MAIN_PROJECT_PATH)));
        }
    }

答案 1 :(得分:6)

通常你不需要扩展TestCase,但我认为不应该导致这个问题。你能否包括失败测试的来源?

关于更新中的简化项目,请确保将Eclipse配置为将当前工作目录设置为主项目的根目录,而不是测试项目,因此它将找到AndroidManifest.xml和res目录。

答案 2 :(得分:2)

无需创建自定义 RoboelectricTestRunner ,只需使用“运行配置”即可。在 Eclipse 上。在运行测试的配置上,转到类路径选项卡并确保测试所需的库,例如, robolectric-2.2-jar-with-dependencies.jar和android.jar以及项目本身位于用户条目中,且只有Java SDK位于 Bootstrap条目中。< / p>

然后添加此文件:

<强> [project_path] /test/org.robolectric.Config.properties

 manifest=../AndroidManifest.xml