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

需要幫助來使用 Mockito 和 JUnit4 編寫單元測試

Need help to write a unit test using Mockito and JUnit4(需要幫助來使用 Mockito 和 JUnit4 編寫單元測試)
本文介紹了需要幫助來使用 Mockito 和 JUnit4 編寫單元測試的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在使用 Mockito 和 JUnit4 為以下代碼編寫單元測試時需要幫助,

Need help to write a unit test for the below code using Mockito and JUnit4,

public class MyFragmentPresenterImpl { 
      public Boolean isValid(String value) {
        return !(TextUtils.isEmpty(value));
      }
}

我嘗試了以下方法:MyFragmentPresenter mMyFragmentPresenter

I tried below method: MyFragmentPresenter mMyFragmentPresenter

@Before
public void setup(){
    mMyFragmentPresenter=new MyFragmentPresenterImpl();
}

@Test
public void testEmptyValue() throws Exception {
    String value=null;
    assertFalse(mMyFragmentPresenter.isValid(value));
}

但它返回以下異常,

java.lang.RuntimeException: android.text.TextUtils 中的方法 isEmpty沒有被嘲笑.有關詳細信息,請參閱 http://g.co/androidstudio/not-mocked.在android.text.TextUtils.isEmpty(TextUtils.java) at ....

java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked. See http://g.co/androidstudio/not-mocked for details. at android.text.TextUtils.isEmpty(TextUtils.java) at ....

推薦答案

由于JUnit TestCase類不能使用Android相關的API,我們不得不Mock它.
使用 PowerMockito 模擬靜態類.

Because of JUnit TestCase class cannot use Android related APIs, we have to Mock it.
Use PowerMockito to Mock the static class.

在您的測試用例類上方添加兩行,

Add two lines above your test case class,

@RunWith(PowerMockRunner.class)
@PrepareForTest(TextUtils.class)
public class YourTest
{

}

還有設置代碼

@Before
public void setup() {
    PowerMockito.mockStatic(TextUtils.class);
    PowerMockito.when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer<Boolean>() {
        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            CharSequence a = (CharSequence) invocation.getArguments()[0];
            return !(a != null && a.length() > 0);
        }
    });
}

用我們自己的邏輯實現 TextUtils.isEmpty().

That implement TextUtils.isEmpty() with our own logic.

另外,在 app.gradle 文件中添加依賴項.

Also, add dependencies in app.gradle files.

testCompile "org.powermock:powermock-module-junit4:1.6.2"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.2"
testCompile "org.powermock:powermock-api-mockito:1.6.2"
testCompile "org.powermock:powermock-classloading-xstream:1.6.2"

感謝 BehelitException 的回答.

Thanks Behelit's and Exception's answer.

這篇關于需要幫助來使用 Mockito 和 JUnit4 編寫單元測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 操作標簽的多行 EditText)
How to detect the swipe left or Right in Android?(如何在 Android 中檢測向左或向右滑動?)
Prevent dialog dismissal on screen rotation in Android(防止在Android中的屏幕旋轉對話框解除)
How do I handle ImeOptions#39; done button click?(如何處理 ImeOptions 的完成按鈕點擊?)
How do you set EditText to only accept numeric values in Android?(您如何將 EditText 設置為僅接受 Android 中的數值?)
主站蜘蛛池模板: 精品亚洲一区二区 | 午夜精品久久久久久久99黑人 | 高清成人免费视频 | 国产精品99久久久久久久vr | 欧美久久国产 | www视频在线观看 | 日本在线黄色 | 精品亚洲一区二区三区四区五区 | 国产三级精品三级在线观看四季网 | 欧美精品成人一区二区三区四区 | 亚洲一区不卡 | 精品国产一区二区三区在线观看 | 一区免费观看 | 午夜视频在线免费观看 | 中文字幕在线免费视频 | 日韩精品999 | 91在线视频播放 | 蜜桃视频在线观看免费视频网站www | 一级黄a视频 | 中文字幕视频在线观看免费 | 欧美日韩亚洲国产 | 一区二区免费视频 | 一区二区三区国产 | 中文一区二区 | 青青久久 | 国内自拍第一页 | 国产成人jvid在线播放 | 色综合天天天天做夜夜夜夜做 | 国产视频h | 午夜天堂 | 成人片在线看 | 欧美日韩亚洲国产 | 毛片99 | 欧美日韩在线播放 | 国产乱码精品一区二区三区五月婷 | 亚洲第一成年免费网站 | 青青草在线视频免费观看 | 热99| 日韩一区二区在线看 | 欧美一区二区三区大片 | 欧美日韩视频 |