用户的Mockito单元测试功能在我的情况做异步任务 [英] User Mockito to unit test a function doing async task in my case

查看:428
本文介绍了用户的Mockito单元测试功能在我的情况做异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android版的项目,我有一个类继承的 HandlerThread

In my Android project, I have a class extends HandlerThread:

public class MyHandlerThread extends HandlerThreads {
   private Handler mHandler;
   …
   public void doAsyncTask(MyAsyncTask task) {
        mHandler = new Handler(this.getLooper());
        mHandler.post(task);
    }
}

上面的函数的参数类型 MyAsyncTask 是一个类继承的Runnable

The above function's parameter type MyAsyncTask is a class extends Runnable:

public abstract class MyAsyncTask implements Runnable {
    @Override
    public void run() {
        doTask();
    }

    public abstract void doTask();
}

我有具有函数使用 MyWorker MyHandlerThread 类:

public class MyWorker {

  public void work() {
      MyHandlerThread handlerThread = new MyHandlerThread();
      handlerThread.start();
      handlerThread.doAsyncTask(new MyAsyncTask() {
           @Override
           doTask() {
               int responseCode = sendDataToServer();
           }
      });
  }
}

我想用 的Mockito进行单元测试工作() MyWorker 类功能(如检查服务器响应code )。如何做到这一点在的Mockito?

I want to use Mockito to unit test the work() function in MyWorker class (e.g. check the server responseCode). How to do it in Mockito?

推荐答案

你要什么测试? MyWorker MyHandlerThread ?匿名 MyAsyncTask ?所有toegether?可能是一个坏主意,特别是如果匿名 MyAsyncTask 依赖于实际的服务器响应(这将prevent这不是一个很好的单元测试,因为你正在测试一个整体然后系统)。所以,我会分裂整个事情分成部分和单独测试所有这些部件。如果你这样做了,那么你可以检查toegether多个部分对一个真正的服务器与集成测试。

WHAT do you want to test? MyWorker? MyHandlerThread? the anonymous MyAsyncTask? All toegether? Probably a bad idea, especially if the anonymous MyAsyncTask relies on an actual server response (which would prevent this from being a good unit test, since you are testing a whole system then). So, I would split the whole thing into parts and test all these parts seperately. If you have done so, then you can check multiple parts toegether against a real server with an integration tests.

要测试MyHandlerThread,例如,你可以引入一个HandlerFactory,嘲笑的,从而确保了处理程序正确调用。

To test the MyHandlerThread, you could for example introduce a HandlerFactory, mock that and thus ensure that the handler was called correctly.

public class MyHandlerThread extends HandlerThreads {
   private HandlerFactory handlerFactory; // <- Add setter
   …
   public void doAsyncTask(MyAsyncTask task) {
        Handler mHandler = handlerFactory.createHandler(this.getLooper());
        mHandler.post(task);
    }
}

容易测试单位。 MyAsyncTask 是短暂的,抽象的,说实话,我不会测试。不大有收获那里,因为它实际上并不做太多。和 MyWorker ?要看,但你可以,例如,添加getter / setter方法​​ MyHandlerThread ,让您的嘲笑。提取您的匿名类变成一个真正的可能会允许你测试之一,也独立于其他。

Easily testable unit. MyAsyncTask is short and abstract, honestly, I wouldn't test that. Not much to gain there, since it doesn't actually do much. And the MyWorker ? Depends, but you could, for example, add a getter/setter for the MyHandlerThread, allowing you to mock that. Extracting your anonymous class into a real one would probably allow you to test that one, too, independent of the others.

这篇关于用户的Mockito单元测试功能在我的情况做异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆