android - Robolectric getResources() 抛出 RuntimeException (Android)

标签 android unit-testing robolectric

我正在尝试使用 Robolectric 框架为我的 android 项目创建单元测试。 我从一个由 eclipse android 项目向导创建的空 Android 项目开始。

在另一个 java 项目中,我使用这段代码尝试运行基本测试:

@RunWith(RobolectricTestRunner.class)
public class ApiTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void shouldHaveApplicationName() throws Exception {
        String appName = new RobolectricActivity().getResources().getString(R.string.app_name);
        assertEquals(appName, "TestDemo");
    }

    @Test
    public void testSomethingMeaningfulToMyApp() {
        fail("not implemented");
    }

}

我已经按照 Robolectric Quick Start Guide 中的说明配置了所有内容.

但是,当我尝试运行此测试类时,在访问 getResources() 函数的行中出现以下异常:

java.lang.RuntimeException: Did your shadow implementation of a method throw an exception? Refer to the bottom of this stack trace.
at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:86)
at com.xtremelabs.robolectric.RobolectricInternals.methodInvoked(RobolectricInternals.java:50)
at android.content.ContextWrapper.getResources(ContextWrapper.java)
at com.xtremelabs.robolectric.shadows.ShadowContextWrapper.getResources(ShadowContextWrapper.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:78)
at com.xtremelabs.robolectric.RobolectricInternals.methodInvoked(RobolectricInternals.java:50)
at android.content.ContextWrapper.getResources(ContextWrapper.java)
at ApiTest.shouldHaveApplicationName(ApiTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:164)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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:49)
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.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:78)
... 36 more
Caused by: java.lang.ExceptionInInitializerError
at android.os.Build$VERSION.<clinit>(Build.java:95)
at android.content.res.Resources.<clinit>(Resources.java:57)
at com.xtremelabs.robolectric.shadows.ShadowApplication.getResources(ShadowApplication.java:74)
... 41 more
Caused by: java.lang.NullPointerException
at android.os.SystemProperties.<clinit>(SystemProperties.java:35)
... 44 more

似乎影子类设置不正确,我在 google 或 stackoverflow 上没有找到解决这个问题的方法。

你对我有什么建议吗?非常感谢!

最佳答案

您是否尝试过为调用要测试的 Activity 的测试类创建构造函数? 例如:

public class ApiTest {

    public ApiTest() {
        super(MainActivity.class);
    }

    //...the rest of your test class
}

我自己仍在学习如何在 Android 中进行单元测试和模拟,所以如果这对您不起作用,我提前道歉。

关于android - Robolectric getResources() 抛出 RuntimeException (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13729682/

相关文章:

android - 新的 Android Lifecycle Architecture 组件中是否可以在不进行注解处理的情况下观察生命周期事件?

python-3.x - Pytest Cov 报告缺少模拟异常返回

android - 如何打开 robolectric 日志记录

android - 使用 Robolectric 隐藏 TimeZone.getDefault

android - 如何在没有互联网的情况下将经纬度发送到android中的webservice?

Android ListView 图像滞后

android - 测试单击按钮时是否启动了正确的 Activity

php - 给 PHP 对象类型参数一个默认值

java - 如何对成熟的 Android 应用程序运行集成测试?

android - 我如何将 Looper 与 asyncTask 一起使用?