Robolectric测试报错集合

1、JsonObject not mocked

这是因为JsonObject属于Android SDK,我们如果做单元测试需要用到它,那就需要引入json.jar包。所以,

第一步:下载json.jar包

下载地址:https://mvnrepository.com/artifact/org.json/json

随便选一个日期,我这里下载最新2018年8月13号发布的,如下图所示:

选择下图中的View All会进入一个新的页面,点击|“json-20180813.jar”下载。

第二步:把jar包放进要测试的项目即可。

2、报错Error:(125, 0) Could not set unknown property 'includeAndroidResources'

本地配置:AS 3.0.1 + Robolectric 3.8+gradle 4.4-rc-3+sdk25

报错原因:build.gradle里面是

classpath 'com.android.tools.build:gradle:2.3.3'

但includeAndroidResources是3.0+才有,所以改成

classpath 'com.android.tools.build:gradle:3.0.1'

再次运行,问题解决。

3.java.lang.IllegalStateException: this method should only be called by Robolectric

原因是sdk版本配置有误,在我们设置了sdk时,会先下载对应的robolectric.jar包并安装。如果当前安装的是sdk25对应的jar包,但用的是sdk=27就会报这个错。

4、Unable to resolve artifact: Missing: ---------- 1) org.robolectric:android-all:jar:8.1.0-robolectric-r4458339 Try downloading the file manually from the project website. Then, install it using the command: mvn install:install-file -DgroupId=org.robolectric -DartifactId=android-all -Dversion=8.1.0-robolectric-r4458339 -Dpackaging=jar -Dfile=/path/to/file Alternatively, if you host your own repository you can deploy the file there: mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=android-all -Dversion=8.1.0-robolectric-r4458339 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id] Path to dependency: 1) org.apache.maven:super-pom:pom:2.0 2) org.robolectric:android-all:jar:8.1.0-robolectric-r4458339 ---------- 1 required artifact is missing. for artifact: org.apache.maven:super-pom:pom:2.0 from the specified remote repositories: nexus (http://116.10.196.222:8081/nexus/content/groups/public/)

解决方法:其实上面说得很清楚了,下载对应的jar包然后安装。具体如下:

第一步:https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/8.1.0-robolectric-r4458339/

在该路径下找到我们所要的那个jar包,然后“cmd"->输入”mvn install:install-file -DgroupId=org.robolectric -DartifactId=android-all -Dversion=8.1.0-robolectric-r4458339 -Dpackaging=jar -Dfile=/path/to/file",其中/path/to/file是我们下载下来的这个jar包的路径。

当出现下图时说明安装成功。

5、java.lang.NoClassDefFoundError: Could not initialize class io.objectbox.internal.NativeLibraryLoader

和 java.lang.UnsatisfiedLinkError: Native Library D:\xxx\app\objectbox-windows-x64.dll already loaded in another classloader

sdk设置不正确,要和我们安装的robolectric.jar包版本对应

6、Could not open env for DB (error code -30789)

7、java.lang.RuntimeException: Method i in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details. at android.util.Log.i(Log.java)

很明显,这个是说我们用到了android包中的方法,也就是Log.i,替换成ShadowLog.i方法即可。

8、java.lang.IllegalArgumentException: Don't know what to do with dataSource org.robolectric.shadows.util.DataSource@aa97c9b1 - either add an exception with addException() or media info with addMediaInfo()

这个问题定位在一个播放asserts中某个MP3文件的位置,一开始我以为是找不到这个MP3文件,可是文件明明存在呀。接着我又想会不会是asserts文件夹是错误的呢?于是我在RobolectricTestRunner.class中打了断点去获取相关的配置新,得到下图这个结果:

很明显,这就是我本地的build中对应的文件路径,并且那个MP3文件也是存在的。

最后我找到了真正的原因:那就是我当前所用的sdk是27,所以需要去获取文件的读写权限,因为没有获取到权限,所以才会出现这个错误。

9、Robolectric测试类内部的自定义广播接收者报错:xxx is not an enclosing class,这个xxx就是我的自定义广播名称。这是什么原因呢?

我最开始的代码是这样子写的:

@Test
public void onCreate() throws Exception {
    ShadowApplication shadowApplication = ShadowApplication.getInstance();
    Intent intent = new Intent(UPDATE_COLLECT_MAIN);

    //测试是否注册广播接收者
    assertTrue(shadowApplication.hasReceiverForIntent(intent));
    //以下测试广播接收者的处理逻辑是否正确
    CollectListPresenter.CollectListReceiver mReceiver = new CollectListPresenter.CollectListReceiver();
    mReceiver.onReceive(RuntimeEnvironment.application, intent);
    //...操作测试忽略
}


// 这是测试类中的自定义广播接收者的代码
public class CollectListReceiver extends BroadcastReceiver {

    // 自定义一个广播接收器
    @Override
    public void onReceive(Context context, Intent intent) {
        //...接受广播后的一些操作
    }
}

其中CollectListPresenter就是这个测试类,而CollectListReceiver自然就是在这个测试类内部的自定义广播接收者。乍一看和我们平时new一个对象没啥区别嘛,实际上这是不对的。所以后面我改成了下面的测试代码,这里只是做个记录:

@Test
public void onCreate() throws Exception {
    ShadowApplication shadowApplication = ShadowApplication.getInstance();
    Intent intent = new Intent(UPDATE_COLLECT_MAIN);

    //测试是否注册广播接收者
    assertTrue(shadowApplication.hasReceiverForIntent(intent));
    //以下测试广播接收者的处理逻辑是否正确
    CollectListPresenter.CollectListReceiver mReceiver = presenter.new CollectListReceiver();
    mReceiver.onReceive(RuntimeEnvironment.application, intent);
    //...操作测试忽略
}

其中上面的presenter是我们事先new 好的CollectListPresenter对象。

发布了63 篇原创文章 · 获赞 9 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/shan286/article/details/86309185