UnitTest＋Robolectric3.0の環境構築

# Robolectric 3.0

## Setup

Android Studio 1.2 で [Robolectric3.0](http://robolectric.org/)を準備する.

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

```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](http://tools.android.com/tech-docs/unit-testing-support)

build.gradleの全容は下記.
[app/build.gradle](https://github.com/YukiMatsumura/RobolectricSample/blob/master/app/build.gradle) 

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

### テストケースを書く

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

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

テストケースの全容は下記.
[MainActivityTest.java](https://github.com/YukiMatsumura/RobolectricSample/blob/master/app/src/test/java/yuki/m/android/robolectricsample/MainActivityTest.java) 


### テスト実行

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

```text
  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](https://github.com/robolectric/robolectric/issues/1648)
[http://robolectric.org/getting-started/ #Note for Mac Users](http://robolectric.org/getting-started/)


### Point

```gradle
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'
}
```

```java
@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");
    }
}
```

以上.