在我的情况下,用户 Mockito 对执行异步任务的函数进行单元测试

User Mockito to unit test a function doing async task in my case(在我的情况下,用户 Mockito 对执行异步任务的函数进行单元测试)
本文介绍了在我的情况下,用户 Mockito 对执行异步任务的函数进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的 Android 项目中,我有一个类扩展了 处理线程:

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 类:

I have a MyWorker class which has a function uses MyHandlerThread class:

public class MyWorker {

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

我想使用 Mockito 对 中的 work() 函数进行单元测试MyWorker 类(例如检查服务器 responseCode).如何在 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?

推荐答案

你想测试什么?我的工人?MyHandlerThread?匿名 MyAsyncTask?一起来?可能是个坏主意,特别是如果匿名 MyAsyncTask 依赖于实际的服务器响应(这会阻止这成为一个好的单元测试,因为那时您正在测试整个系统).所以,我会把整个事情分成几个部分,分别测试所有这些部分.如果您已经这样做了,那么您可以通过集成测试将多个部分与真实服务器一起检查.

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 呢?取决于,但你可以,例如,为 MyHandlerThread 添加一个 getter/setter,允许你模拟它.将您的匿名类提取到一个真实的类中可能会让您也可以独立于其他类来测试该类.

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 对执行异步任务的函数进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Android Mocking a Dagger2 injected dependency for a Espresso test(Android 为 Espresso 测试模拟 Dagger2 注入依赖项)
Mock object in Android Unit test with kotlin - any() gives null(使用 kotlin 在 Android 单元测试中模拟对象 - any() 给出 null)
How to mock Context using Mockito?(如何使用 Mockito 模拟上下文?)
Unit test Android, getString from resource(单元测试Android,从资源中获取字符串)
Mockito - what does verify method do?(Mockito - 验证方法有什么作用?)
Unit Testing in Retrofit for Callback(回调改造中的单元测试)