Help us understand the problem. What is going on with this article?

Robolectricのテストケースでassetsからファイルを読み込む

More than 5 years have passed since last update.

assetsからテスト用のJSONデータ読み込もうとして上手くいかず苦労した。
読み込むコードは↓のような感じ。

package com.example.horie1024.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;

import java.io.InputStream;
import static org.junit.Assert.assertNotNull;

@org.robolectric.annotation.Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class HogeTest {

    @Test
    public void readAssets() throws IOException {
        InputStream is = Robolectric.application.getAssets().open("test.json");
        assertNotNull(is);
    }
}

このコードの場合build/intermediates/bundles/debug以下のassetsを見に行ってしまっていた。
解決方法を探すと、RobolectricTestRunner.classを継承してassetsのpathを指定する方法があったので試してみる。

RobolectricTestRunner.classを継承してgetAppManifestをOverrideし、System.setPropertyでandroid.assetsのパスを指定する。

package com.example.horie1024.test;

import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

public class MyTestRunner extends RobolectricTestRunner {

    public MyTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config) {

        System.setProperty("android.assets", "./src/androidTest/assets");
        return super.getAppManifest(config);
    }
}

テストランナーを置き換える。

package com.example.horie1024.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;

import java.io.InputStream;
import static org.junit.Assert.assertNotNull;

@org.robolectric.annotation.Config(emulateSdk = 18)
@RunWith(MyTestRunner.class)
public class HogeTest {

    @Test
    public void readAssets() throws IOException {
        InputStream is = Robolectric.application.getAssets().open("test.json");
        assertNotNull(is);
    }
}

これで上手く読み込めた。

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away