1.当使用Mockito.verify(mock).performLogin("chenyou", anyString());这个语句时报如下错误,正确的做法是
Mockito.verify(mock).performLogin(eq("chenyou"),anyString());当使用匹配器时,所有参数必须由匹配器提供。


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

2.当出现如下错误的时候有两种情况:一种是在该方法内调用mock对象方法之前已经return了,一种是mock对象没有替换掉正式代码里的对象


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
  • 出现如上No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml的办法如下:

第一步:


解决办法 第一步

第二步:


解决办法 第二步

如果步骤2没有自动设置这个的话,这个也需要手动设置


如果上面两步都不行 该看这里了
  • 第二个神坑级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.

这个问题有点奇怪,这个测试类中我并没有用到Robolectric和Mock但是运行时报错了,解决方式是在类名前面添加这两行代码

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容