久久久久久久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 中的數值?)
主站蜘蛛池模板: 综合天天久久 | 九九九久久国产免费 | 久久一区二区免费视频 | 日韩在线免费视频 | 精品一区国产 | 久久久久国产精品午夜一区 | 中文字幕在线国产 | 欧美成人激情 | 亚洲人成在线播放 | 伊人色综合久久久天天蜜桃 | 91日日 | 九色在线观看 | 中文字幕av中文字幕 | 男女免费观看在线爽爽爽视频 | 99这里只有精品视频 | 91精品久久久久久久久久 | 色久影院| 久久久久国产精品一区二区 | 欧美天堂一区 | 日韩一区二区三区视频 | 日韩中文字幕 | 久久久久久久91 | 亚洲精品久久久一区二区三区 | 看片国产 | 91资源在线观看 | 超碰伊人 | 久草免费在线视频 | 91xxx在线观看 | cao在线| 久久伊人青青草 | av一级久久 | 久草中文在线 | 精品欧美 | 日韩成年人视频在线 | 超碰电影| 日本精品一区二区在线观看 | 97av视频在线 | 毛片网在线观看 | 亚洲美女一区二区三区 | 国产sm主人调教女m视频 | 精品1区2区3区 |