5
3

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.

AndroidのJUnitテストをEnclosedを使ってシュッとする

Last updated at Posted at 2016-06-08

Androidのテストを書いてるとだんだんごちゃごちゃしてくる

汚くなるテストクラスの例
public class HogeUnitTest {
    @Test
    public void hogeTest() {
        HogeStatus hogeStatus = new HogeStatus();
        assertTrue(hogeStatus.isRunning());
        assertFalse(hogeStatus.isCanceled());
        //cancel
        hogeStatus.cancel();
        assertFalse(hogeStatus.isRunning());
        assertTrue(hogeStatus.isCanceled());
        //finish
        hogeStatus = new HogeStatus();
        hogeStatus.finish();
        assertFalse(hogeStatus.isRunning());
        assertFalse(hogeStatus.isCanceled());
    }
    @Test
    public void mogeTest() {
        Moge moge = new Moge();
        assertNull(moge.someProperty1);
        assertNotNull(moge.someProperty2);
        //calculate
        moge.calculate()
        assertEquals(moge.value, 10);
    }
}

コメントとかをつけて頑張ってみても汚くなる...
Rspecのdescribeみたいにグループ化とか出来たら捗るのに...そう思ったことはありませんか?

Enclosedを使うことでシュッとした感じにテストを書くことができます!

綺麗になったテストクラスの例
@RunWith(Enclosed.class)
public class HogeUnitTest {
    @RunWith(CustomRobolectricTestRunner.class) //例では必要ないけど必要な場合はここに書く
    @Config(constants = BuildConfig.class)
    public static class HogeTest {
        @Test
        public void start() {
            // given
            HogeStatus hogeStatus = new HogeStatus();
            // then
            assertTrue(hogeStatus.isRunning());
            assertFalse(hogeStatus.isCanceled());
        }
        @Test
        public void cancel() {
            // given
            HogeStatus hogeStatus = new HogeStatus();
            // when
            hogeStatus.cancel();
            // then
            assertFalse(hogeStatus.isRunning());
            assertTrue(hogeStatus.isCanceled());
        }
        @Test
        public void finish() {
            // given
            HogeStatus hogeStatus = new HogeStatus();
            // when
            hogeStatus.finish();
            // then
            assertFalse(hogeStatus.isRunning());
            assertFalse(hogeStatus.isCanceled());
        }
    }
    public static class MogeTest {
        @Test
        public void calculate() {
            // given
            Moge moge = new Moge();
            // when
            moge.calculate()
            // then
            assertNull(moge.someProperty1);
            assertNotNull(moge.someProperty2);
            assertEquals(moge.value, 10);
        }
    }
}

こんな風に書くと実行結果もいい感じに分割されて解りやすい&&モチベーションアップに繋がるのでEnclosedおすすめです

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?