我有一个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。
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文件
shadows=org.robolectric.shadows.ShadowMultiDex有了以上这些,我就进入了我的测试类,并将ShadowMultiDex包含在我的小测试中。
@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。我正在寻找的是看看其他人做了什么来实际解决这个问题,并让它运行。
发布于 2015-03-24 15:20:10
你试过使用Android单元测试支持 + Multidex + Robolectric + Mockito吗?
我做了一些小小的改动:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
try{
MultiDex.install(this);
} catch (RuntimeException ignore){
//Multidex error when running Robolectric tests => ignore.
}
} 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);
}希望能帮上忙。
https://stackoverflow.com/questions/29022549
复制相似问题