首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用程序无法运行Robolectric单元测试

应用程序无法运行Robolectric单元测试
EN

Stack Overflow用户
提问于 2015-03-12 23:59:22
回答 1查看 1.2K关注 0票数 1

我有一个Android应用程序,它使用了如此多的库,现在我需要使用MultiDex-ing。不幸的是,当我引入Multide行时,我现有的Robolectric单元测试不再运行,甚至无法启动。在我加入多个德兴之前,他们跑得很好。

参考github上最初的bug帖子

https://github.com/robolectric/robolectric/issues/1328

和建议的解决方案

https://github.com/robolectric/robolectric/pull/1457

我仍然无法让我的单元测试运行。

下面是我的build.gradle文件版本1的一个片段,在这里我试图获得最新的频域快照,以及我可以在代码中使用的ShadowMultiDex.java。

代码语言:javascript
复制
buildscript {
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
    dependencies {
        classpath 'org.robolectric:robolectric-gradle-plugin:0.14.+'
    }
}

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

apply plugin: 'android'
apply plugin: 'robolectric'
apply plugin: 'com.android.application'

...

android {
    // other things
    ...

    sourceSets {
        androidTest {
            setRoot('src/test')
            resources.srcDirs = ['assets']
        }
    }
}

dependencies {
    // regular
    ...

    // unit testing
    androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
    androidTestCompile('com.squareup:fest-android:1.0.+') {
        exclude group: 'com.android.support'
    }
    androidTestCompile 'com.google.dexmaker:dexmaker:1.+'


    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'org.robolectric:android-all:+'
    androidTestCompile 'org.robolectric:shadows-multidex:3.0-SNAPSHOT'
    androidTestCompile('org.robolectric:robolectric:3.0-SNAPSHOT') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }

我还创建了一个包含以下内容的org.robolectric.Config.properties文件

代码语言:javascript
复制
shadows=org.robolectric.shadows.ShadowMultiDex

有了以上这些,我就进入了我的测试类,并将ShadowMultiDex包含在我的小测试中。

代码语言:javascript
复制
@RunWith(RobolectricGradleTestRunner.class)
@Config(shadows = {ShadowMultiDex.class})
public class AppVersionTest extends MyBaseTest {

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @Test
    public void testIsDefaultTrue() {
        // setup
        AppVersion appVersion = new AppVersion(null);

        // run
        boolean result = appVersion.isDefault();

        // verify
        assertThat(result).isTrue();
    }
}

当我运行单元测试时,我仍然会得到一个用于设置应用程序及其元数据的NullPointerException。我正在寻找的是看看其他人做了什么来实际解决这个问题,并让它运行。

EN

回答 1

Stack Overflow用户

发布于 2015-03-24 15:20:10

你试过使用Android单元测试支持 + Multidex + Robolectric + Mockito吗?

我做了一些小小的改动:

  • 我不能只执行命令行的单元测试。
  • 需要使用Mockito的1.9.0版本,因为版本1.9.5不能很好地与Robolectric + Multidex一起工作。(如果你成功了,请分享:)。我在我的仪器测试中使用1.9.5版本。
  • 在我的attachBaseContext上实现MainApplication如下:
代码语言:javascript
复制
  @Override
  protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    try{
        MultiDex.install(this);
    } catch (RuntimeException ignore){
        //Multidex error when running Robolectric tests => ignore.
    }
  } 
  • 必须排除重复依赖项(检查这条柱子)
  • 我使用的是一个自定义的Robolec测距运行程序,其中我刚刚实现了它的构造函数(以设置资源文件的正确路径):
代码语言:javascript
复制
public RobolectricCustomRunner(Class<?> testClass) throws InitializationError     {
    super(testClass);
    String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
    String intermediatesPath = "build/intermediates";

    System.setProperty("android.package", BuildConfig.APPLICATION_ID);
    System.setProperty("android.manifest", intermediatesPath + "/manifests/full/" + buildVariant + "/AndroidManifest.xml");
    System.setProperty("android.resources", intermediatesPath + "/res/" + buildVariant);
    System.setProperty("android.assets", intermediatesPath + "/assets/" + buildVariant);
}

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29022549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档