Robolectric 3.0无法测试启动HandlerThread的函数

时间:2015-12-11 15:30:33

标签: android unit-testing robolectric

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

0 个答案:

没有答案