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


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)