久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在我的情況下,用戶 Mockito 對(duì)執(zhí)行異步任務(wù)的函

User Mockito to unit test a function doing async task in my case(在我的情況下,用戶 Mockito 對(duì)執(zhí)行異步任務(wù)的函數(shù)進(jìn)行單元測(cè)試)
本文介紹了在我的情況下,用戶 Mockito 對(duì)執(zhí)行異步任務(wù)的函數(shù)進(jìn)行單元測(cè)試的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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ì) 中的 work() 函數(shù)進(jìn)行單元測(cè)試MyWorker 類(例如檢查服務(wù)器 responseCode).如何在 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event(EditText:禁用文本選擇處理程序單擊事件上的粘貼/替換菜單彈出)
Multiline EditText with Done SoftInput Action Label on 2.3(2.3 上帶有完成 SoftInput 操作標(biāo)簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測(cè)向左或向右滑動(dòng)?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉(zhuǎn)對(duì)話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點(diǎn)擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設(shè)置為僅接受 Android 中的數(shù)值?)
主站蜘蛛池模板: 天天干天天操天天射 | 99久久99久久精品国产片果冰 | 午夜婷婷激情 | 亚洲五码久久 | 中文天堂在线观看 | 国产精品美女久久久久久免费 | 国产激情视频在线观看 | 四虎成人免费视频 | 欧美一级大片免费观看 | 久久宗合色 | 黄色一级大片在线免费看产 | 国产免费福利 | 黄色大片视频 | 91 在线| 欧美一区二区另类 | 黄色在线观看 | 99精品在线观看 | 亚洲一区二区精品视频在线观看 | 久久一区精品 | 久久婷婷国产香蕉 | 一区在线视频 | 色婷婷综合成人av | 久久精品国内 | 自拍偷拍第一页 | 国产精品成人一区二区三区夜夜夜 | 狠狠骚| 国产一区二区 | 日韩在线免费 | 91国产视频在线 | 成人字幕网zmw | 日韩欧美在线观看视频 | 欧美精品一区二区在线观看 | 青青久久 | 欧美性受xxx | 精品视频在线免费观看 | 日韩精品一区二区三区 | 视频一区在线 | 免费毛片网 | 日本久久黄色 | 日日操操 | 中文字幕精品一区二区三区精品 |