我有一个简单的课程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
实例),还是我错过了什么?如何摆脱这个问题?