1.當(dāng)使用Mockito.verify(mock).performLogin("chenyou", anyString());這個(gè)語句時(shí)報(bào)如下錯(cuò)誤,正確的做法是
Mockito.verify(mock).performLogin(eq("chenyou"),anyString());當(dāng)使用匹配器時(shí),所有參數(shù)必須由匹配器提供。


When using matchers, all arguments have to be provided by matchers

2.當(dāng)出現(xiàn)如下錯(cuò)誤的時(shí)候有兩種情況:一種是在該方法內(nèi)調(diào)用mock對(duì)象方法之前已經(jīng)return了,一種是mock對(duì)象沒有替換掉正式代碼里的對(duì)象


Actually, there were zero interactions with this mock
3.神坑:Robolectric配置問題
  • 神坑1:編譯不通過,編譯沖突
    Error:Conflict with dependency 'com.google.guava:guava' in project ':app'. Resolved versions for app (18.0) and test app (20.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
    編譯沖突問題

    解決辦法:
    testCompile('org.robolectric:robolectric:3.3.2', {
    exclude group: 'com.google.guava', module: 'guava'
    })
    編譯沖突解決辦法

神坑2:


No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml
  • 出現(xiàn)如上No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml的辦法如下:

第一步:


解決辦法 第一步

第二步:


解決辦法 第二步

如果步驟2沒有自動(dòng)設(shè)置這個(gè)的話,這個(gè)也需要手動(dòng)設(shè)置


如果上面兩步都不行 該看這里了
  • 第二個(gè)神坑級(jí)Robolectric配置問題
神坑2 找不到資源ID

解決辦法:添加@Config(constants = BuildConfig.class) 如下圖:

找不到資源ID的解決辦法

4.java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.

Method getMainLooper in android.os.Looper not mocked.

這個(gè)問題有點(diǎn)奇怪,這個(gè)測(cè)試類中我并沒有用到Robolectric和Mock但是運(yùn)行時(shí)報(bào)錯(cuò)了,解決方式是在類名前面添加這兩行代碼

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容