我有一个Job
扩展HandlerThread的简单类:
public class Job extends HandlerThread{ public Job(String name) { super(name); } ... }
然后,我有一个JobUtils
类,它具有获取Job
&start()
它的功能:
public JobUtils { public JobUtils() { } // I unit test this function in my test class below public Job getAndStartJob(String name) { Job job = new Job(name); job.start(); } }
我在单元测试中使用Robolectric 3.0,我测试JobUtils
类的getAndStartJob(String name)
功能:
@RunWith(RobolectricTestRunner.class) public class TestJobUtils{ @Test public void testGetAndStartJob() { JobUtils jobUtils = new JobUtils(); // error here jobUtils.getAndStartJob(“test-job”); … } }
当我运行我的单元测试代码时,我收到了错误
Exception in thread "test-job” java.lang.NullPointerException at org.robolectric.shadows.ShadowLooper.getMainLooper(ShadowLooper.java:70) at org.robolectric.shadows.ShadowLooper.doLoop(ShadowLooper.java:85) at org.robolectric.shadows.ShadowLooper.loop(ShadowLooper.java:76) at android.os.Looper.loop(Looper.java) at android.os.HandlerThread.run(HandlerThread.java:60)
看起来Robolectric无法启动HandlerThread
(job
我的代码中的实例),还是我错过了什么?如何摆脱这个问题?