ShadowSystemClock NullPointerException

时间:2013-12-23 14:12:00

标签: android nullpointerexception tdd robolectric

我在Robolectric测试案例中遇到问题,我的CustomApllication.class实例化,并且正好使用此行private long value = SystemClock.elapsedRealtime()。我通过简单地扩展DeckardApplication:

,使用Decard示例项目完成了一些测试
public class DeckardApplication extends Application {

    private long value = SystemClock.elapsedRealtime();

}

导致完全相同的错误:

Caused by: java.lang.NullPointerException
    at org.robolectric.shadows.ShadowLooper.getMainLooper(ShadowLooper.java:59)
    at android.os.Looper.getMainLooper(Looper.java)
    at org.robolectric.Robolectric.getUiThreadScheduler(Robolectric.java:1244)
    at org.robolectric.shadows.ShadowSystemClock.now(ShadowSystemClock.java:15)
    at org.robolectric.shadows.ShadowSystemClock.uptimeMillis(ShadowSystemClock.java:30)
    at org.robolectric.shadows.ShadowSystemClock.elapsedRealtime(ShadowSystemClock.java:35)
    at android.os.SystemClock.elapsedRealtime(SystemClock.java)
    at com.example.DeckardApplication.<init>(DeckardApplication.java:8)
    at java.lang.Class.newInstance(Class.java:374)
    at org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:46)
    at org.robolectric.DefaultTestLifecycle.createApplication(DefaultTestLifecycle.java:13)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:100)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:404)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:220)
    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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
    ... 2 more

如何处理此错误?我是否需要提供一些CustomShadowSystemClock实现?

1 个答案:

答案 0 :(得分:1)

提供自定义ShadowSystemClock的解决方案。示例如下:

@Implements(value = SystemClock.class, callThroughByDefault = true)
    public static class MyShadowSystemClock {
        public static long elapsedRealtime() {
            return 0;
        }
    }

并将其应用于测试@Config(shadows = { MyShadowSystemClock.class})

但我还在等待更好的解决方案。