测试执行期间如何从资产文件夹访问文件?

尤金

单元测试执行期间如何从资产文件夹访问文件?我的项目是使用Gradle构建的,我使用Robolectric运行测试。似乎gradle正在识别assets

在此处输入图片说明

这就是我努力读取文件的方式:

public String readFileFromAssets(String fileName) throws IOException {
    InputStream stream = getClass().getClassLoader().getResourceAsStream("assets/" + fileName);
    Preconditions.checkNotNull(stream, "Stream is null");
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    return IOUtils.toString(reader);
}

stream总是如此null我尝试了许多不同的方法,即使用不同的方法定义了文件的路径。

提前非常感谢您。

Chk0nDanger

基本上,您必须使用Context读取资产。ClassLoader由于资源不在类路径中,因此无法加载资源我不确定您如何运行Robolectric测试用例。这是我在Android Studio和gralde命令中都能实现的方法。

我添加了单独的app-unit-test模块以在应用程序项目中运行Robolectric测试用例。使用正确的构建配置和custom 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" } // Fix 'com.android.support:*' package not found issue
    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'
    /* FIXME : prevent Stub! error
        testCompile files(appModule.plugins.findPlugin("com.android.application").getBootClasspath())
        */
    compile project(':app')
}

添加自定义RobolectricTestRunner以调整文件路径。查看资产路径。

public class MyRobolectricTestRunner extends RobolectricTestRunner {

    private static final String APP_MODULE_NAME = "app";

    /**
     * Creates a runner to run {@code testClass}. Looks in your working directory for your AndroidManifest.xml file
     * and res directory by default. Use the {@link org.robolectric.annotation.Config} annotation to configure.
     *
     * @param testClass the test class to be run
     * @throws org.junit.runners.model.InitializationError if junit says so
     */
    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);
    }

}

我按照这个博客来做。

完整的示例代码在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

移动 Jenkins 作业在作业执行期间出错“找不到文件或文件夹”

来自分类Dev

SonarQube 扫描仪执行期间出错。“SonarTest”不存在文件夹“src”

来自分类Dev

无法访问资产子文件夹中的文件

来自分类Dev

无法访问资产子文件夹中的文件

来自分类Dev

如何访问资产文件夹中的图像文件?[角7]

来自分类Dev

从非活动类访问资产文件夹

来自分类Dev

如何从资产子文件夹读取PDF文件

来自分类Dev

如何加载资产文件夹中的属性文件?

来自分类Dev

如何处理“资产”文件夹中的文件?

来自分类Dev

如何打开 KML 文件(来自资产文件夹)

来自分类Dev

如何访问和查询复制到资产文件夹的数据库?

来自分类Dev

如何在React Native中访问其他文件夹中的资产?

来自分类Dev

如何访问和查询复制到资产文件夹的数据库?

来自分类Dev

尝试访问资产文件夹中的子文件夹

来自分类Dev

尝试访问资产文件夹中的子文件夹

来自分类Dev

从资产文件夹uwp写入文件

来自分类Dev

从资产文件夹打开HTML文件

来自分类Dev

从资产文件夹uwp写入文件

来自分类Dev

如何使用CMake将测试可执行文件放入单独的文件夹中?

来自分类Dev

如何通过文件夹iOS从资产获取图像

来自分类Dev

如何从资产文件夹中为ProgressDialog添加字体?

来自分类Dev

如何获得资产文件夹的直接路径

来自分类Dev

如何通过文件夹iOS从资产获取图像

来自分类Dev

如何从资产或原始文件夹查看pdf?

来自分类Dev

如何从Web默认文件夹以外的其他文件夹加载资产?

来自分类Dev

Docker run --mount使所有文件在运行期间位于不同的文件夹中

来自分类Dev

从资产文件夹获取文件夹列表

来自分类Dev

如何访问AppData文件夹?

来自分类Dev

如何访问AppData文件夹?

Related 相关文章

  1. 1

    移动 Jenkins 作业在作业执行期间出错“找不到文件或文件夹”

  2. 2

    SonarQube 扫描仪执行期间出错。“SonarTest”不存在文件夹“src”

  3. 3

    无法访问资产子文件夹中的文件

  4. 4

    无法访问资产子文件夹中的文件

  5. 5

    如何访问资产文件夹中的图像文件?[角7]

  6. 6

    从非活动类访问资产文件夹

  7. 7

    如何从资产子文件夹读取PDF文件

  8. 8

    如何加载资产文件夹中的属性文件?

  9. 9

    如何处理“资产”文件夹中的文件?

  10. 10

    如何打开 KML 文件(来自资产文件夹)

  11. 11

    如何访问和查询复制到资产文件夹的数据库?

  12. 12

    如何在React Native中访问其他文件夹中的资产?

  13. 13

    如何访问和查询复制到资产文件夹的数据库?

  14. 14

    尝试访问资产文件夹中的子文件夹

  15. 15

    尝试访问资产文件夹中的子文件夹

  16. 16

    从资产文件夹uwp写入文件

  17. 17

    从资产文件夹打开HTML文件

  18. 18

    从资产文件夹uwp写入文件

  19. 19

    如何使用CMake将测试可执行文件放入单独的文件夹中?

  20. 20

    如何通过文件夹iOS从资产获取图像

  21. 21

    如何从资产文件夹中为ProgressDialog添加字体?

  22. 22

    如何获得资产文件夹的直接路径

  23. 23

    如何通过文件夹iOS从资产获取图像

  24. 24

    如何从资产或原始文件夹查看pdf?

  25. 25

    如何从Web默认文件夹以外的其他文件夹加载资产?

  26. 26

    Docker run --mount使所有文件在运行期间位于不同的文件夹中

  27. 27

    从资产文件夹获取文件夹列表

  28. 28

    如何访问AppData文件夹?

  29. 29

    如何访问AppData文件夹?

热门标签

归档