30
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnitTest+Robolectric3.0の環境構築

Last updated at Posted at 2015-05-21

Robolectric 3.0

Setup

Android Studio 1.2 で Robolectric3.0を準備する.

build.gradle

モジュールのbuild.gradleに下記ライブラリを追加.

    // UnitTest用.
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"

    // Robolectric用
    testCompile 'org.robolectric:robolectric:3.0-rc2'

Android gradle 1.1以降, AndroidStudioからのUnitTest実行がサポートされた.
http://tools.android.com/tech-docs/unit-testing-support

build.gradleの全容は下記.
app/build.gradle

Build VariantsのTest ArtifactをUnit Testsに設定し, test/javaでユニットテスト用のフォルダを作成しておく.

テストケースを書く

テストケースのRunnerにはRobolectricGradleTestRunner.classを指定.
ConfigアノテーションでGradleの出力パスをRobolectricに伝えるためBuildConfig.classを指定する.

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21)
public class MyTest {

テストケースの全容は下記.
MainActivityTest.java

テスト実行

現状だとテストを実行するには一手間必要である. そのままJUnitを実行すると次のエラーが発生する.

  java.io.FileNotFoundException: build/intermediates/bundles/debug/AndroidManifest.xml (No such file or directory)
  java.lang.UnsupportedOperationException: Robolectric does not support API level 1.

これを解決するためにRun configurationsからRobolectricを実行するプロファイルのworking directory$MODULE_DIR$を指定する.

参考:
https://github.com/robolectric/robolectric/issues/1648
http://robolectric.org/getting-started/ #Note for Mac Users

Point

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'

    // Unit test support on AndroidStudio. Android gradle version 1.1 or later Required.
    //  - see. http://tools.android.com/tech-docs/unit-testing-support
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"

    // If you are on a Mac, you will probably need to configure the default JUnit test runner configuration
    // in order to work around a bug where Android Studio does not set the working directory to the module being tested.
    // This can be accomplished by editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$.
    //  - Resolved java.io.FileNotFoundException: build/intermediates/bundles/debug/AndroidManifest.xml (No such file or directory)
    //  - Resolved java.lang.UnsupportedOperationException: Robolectric does not support API level 1.
    //  - see. https://github.com/robolectric/robolectric/issues/1648
    //  - see. http://robolectric.org/getting-started/#Note for Mac Users
    testCompile 'org.robolectric:robolectric:3.0-rc2'
}
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21)
public class MainActivityTest {
    @Before
    public void setUp() throws Exception {
        ShadowLog.stream = System.out;
    }

    @Test
    public void HelloRobolectric() throws Exception {
        final MainActivity activity = Robolectric.setupActivity(MainActivity.class);
        activity.onCreate(null);
        Log.d("yuki", "Hello Robolectric");
    }
}

以上.

30
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?