gpt4 book ai didi

android - ShadowSystemClock NullPointerException

转载 作者:行者123 更新时间:2023-11-30 03:12:14 25 4
gpt4 key购买 nike

在 Robolectric 测试用例中,我在实例化我的 CustomApllication.class 时遇到问题,并且恰好是这一行 private long value = SystemClock.elapsedRealtime()。我使用 Decard 做了一些测试通过简单地扩展 DeckardApplication 的示例项目:

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 实现?

最佳答案

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

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

然后在测试中应用它 @Config(shadows = { MyShadowSystemClock.class})

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

关于android - ShadowSystemClock NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com