在我的 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模板网!
EditText:禁用文本选择处理程序单击事件上的粘贴EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本选择处理程序单击事件上的粘贴/替换菜
2.3 上带有完成 SoftInput 操作标签的多行 EditTextMultiline EditText with Done SoftInput Action Label on 2.3(2.3 上带有完成 SoftInput 操作标签的多行 EditText)
如何在 Android 中检测向左或向右滑动?How to detect the swipe left or Right in Android?(如何在 Android 中检测向左或向右滑动?)
防止在Android中的屏幕旋转对话框解除Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋转对话框解除)
如何处理 ImeOptions 的完成按钮点击?How do I handle ImeOptions#39; done button click?(如何处理 ImeOptions 的完成按钮点击?)
您如何将 EditText 设置为仅接受 Android 中的数值How do you set EditText to only accept numeric values in Android?(您如何将 EditText 设置为仅接受 Android 中的数值?)