基本上,您必须使用
Context
来读取资源。您不能使用
ClassLoader
加载资源,因为它不在类路径中。我不确定您如何运行Robolectric测试用例。以下是我在Android Studio和gradle命令中实现的方法。
我添加了一个单独的app-unit-test模块来运行应用程序项目中的Robolectric测试用例。通过适当的构建配置和自定义
RobolectricTestRunner
,以下测试用例将通过。
@Config
@RunWith(MyRobolectricTestRunner.class)
public class ReadAssetsTest {
@Test
public void test_ToReadAssetsFileInAndroidTestContext() throws IOException {
ShadowApplication application = Robolectric.getShadowApplication();
Assert.assertNotNull(application);
InputStream input = application.getAssets().open("b.xml");
Assert.assertNotNull(input);
}
}
app-unit-test/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.1'
}
}
apply plugin: 'java'
evaluationDependsOn(':app')
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
repositories {
maven { url "$System.env.ANDROID_HOME/extras/android/m2repository" }
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
testCompile 'junit:junit:4.8.2'
testCompile('org.robolectric:robolectric:2.4') {
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'
exclude group: 'com.android.support', module: 'support-v4'
}
testCompile('com.squareup:fest-android:1.0.+') {
exclude group: 'com.android.support', module: 'support-v4'
}
testCompile 'org.mockito:mockito-core:1.10.10'
def appModule = project(':app')
testCompile(appModule) {
exclude group: 'com.google.android'
exclude module: 'dexmaker-mockito'
}
testCompile appModule.android.applicationVariants.toList().first().javaCompile.classpath
testCompile appModule.android.applicationVariants.toList().first().javaCompile.outputs.files
testCompile 'com.google.android:android:4.1.1.4'
compile project(':app')
}
添加自定义的RobolectricTestRunner以调整文件路径。
查看资产路径。
public class MyRobolectricTestRunner extends RobolectricTestRunner {
private static final String APP_MODULE_NAME = "app";
public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
System.out.println("testclass="+testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
String userDir = System.getProperty("user.dir", "./");
File current = new File(userDir);
String prefix;
if (new File(current, APP_MODULE_NAME).exists()) {
System.out.println("Probably running on AndroidStudio");
prefix = "./" + APP_MODULE_NAME;
}
else if (new File(current.getParentFile(), APP_MODULE_NAME).exists()) {
System.out.println("Probably running on Console");
prefix = "../" + APP_MODULE_NAME;
}
else {
throw new IllegalStateException("Could not find app module, app module should be \"app\" directory in the project.");
}
System.setProperty("android.manifest", prefix + "/src/main/AndroidManifest.xml");
System.setProperty("android.resources", prefix + "/src/main/res");
System.setProperty("android.assets", prefix + "/src/androidTest/assets");
return super.getAppManifest(config);
}
}
我按照这个博客的指示去做了。
完整的示例代码可以在
这里找到。