問(wèn)題描述
在我的 Android 項(xiàng)目中,我有一個(gè)類擴(kuò)展了 處理線程:
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);
}
}
上述函數(shù)的參數(shù)類型MyAsyncTask
是一個(gè)類擴(kuò)展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();
}
我有一個(gè) MyWorker
類,它有一個(gè)函數(shù)使用 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 對(duì) 中的
類(例如檢查服務(wù)器 work()
函數(shù)進(jìn)行單元測(cè)試MyWorkerresponseCode
).如何在 Mockito 中做到這一點(diǎn)?
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?
推薦答案
你想測(cè)試什么?我的工人
?MyHandlerThread
?匿名 MyAsyncTask
?一起來(lái)?可能是個(gè)壞主意,特別是如果匿名 MyAsyncTask
依賴于實(shí)際的服務(wù)器響應(yīng)(這會(huì)阻止這成為一個(gè)好的單元測(cè)試,因?yàn)槟菚r(shí)您正在測(cè)試整個(gè)系統(tǒng)).所以,我會(huì)把整個(gè)事情分成幾個(gè)部分,分別測(cè)試所有這些部分.如果您已經(jīng)這樣做了,那么您可以通過(guò)集成測(cè)試將多個(gè)部分與真實(shí)服務(wù)器一起檢查.
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.
要測(cè)試 MyHandlerThread,你可以引入一個(gè) HandlerFactory,模擬它,從而確保處理程序被正確調(diào)用.
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);
}
}
易于測(cè)試的單元.MyAsyncTask
簡(jiǎn)短而抽象,老實(shí)說(shuō),我不會(huì)測(cè)試它.在那里沒(méi)有太多收獲,因?yàn)樗鼘?shí)際上并沒(méi)有多大作用.MyWorker
呢?取決于,但你可以,例如,為 MyHandlerThread
添加一個(gè) getter/setter,允許你模擬它.將您的匿名類提取到一個(gè)真實(shí)的類中可能會(huì)讓您也可以獨(dú)立于其他類來(lái)測(cè)試該類.
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.
這篇關(guān)于在我的情況下,用戶 Mockito 對(duì)執(zhí)行異步任務(wù)的函數(shù)進(jìn)行單元測(cè)試的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!