19. @RunWith(Categories.class)
Suite のサブクラス
18
指定したカテゴリのテストのみ実行
@RunWith(Categories.class)
@SuiteClasses({ FooTest.class, BarTest.class })
@IncludeCategory(FastTests.class)
public class FastTestSuite {}
public class FooTest {
@Category(FastTests.class)
@Test public void test01() { ... }
}
@Category({ FastTests.class, SlowTests.class })
public class BarTest { ... }
20. @RunWith(Parameterized.class)
@RunWith(Parameterized.class)
public class ShuffleTest {
public ShuffleTest(int n, int[] p, int[] c, int a) {
deck = new Deck(n);
this.p = p; this.c = c; this.a = a;
19
}
@Parameters public static List<Object[]> params() {
List<Object[]> params = new ArrayList<Object[]>();
params.add(new Object[]{5, new int[]{3, 3},
new int[]{1, 1}, 4});
return params;
}
@Test public void test() {
for(int i = 0; i < p.length; i++)
deck.shuffle(p[i], c[i]);
assertThat(deck.getTop(), is(a));
}
}
21. @RunWith(Theories.class)
与えられた入力の組み合わせを網羅してテスト
@RunWith(Theories.class)
20 public class FermatTest {
@Theory public void フェルマーの最終定理
(int n, int a, int b, int c)
{
assumeThat(n, is(greaterThan(1)));
BigInteger an = valueOf(a).pow(n),
bn = valueOf(b).pow(n),
cn = valueOf(c).pow(n);
assertThat(an.add(bn), is(not(cn)));
}
@DataPoints public static final int[] data =
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
23. ExpectedException
@Test(expected=...) で満足できない人に
public class BarbatosTest {
22 @Rule
public ExpectedException ee =
ExpectedException.none();
@Test
public void useItem() {
ee.expect(RuntimeException.class);
ee.expectMessage(
"アイテムなんぞ使ってんじゃねぇ!");
player.useItem();
}
}
24. TemporaryFolder
テスト終了時に自動的に削除されるフォルダ
23 public class TmpFileTest {
@Rule
public TemporaryFolder tmp =
new TemporaryFolder();
@Test
public void test() {
File f = tmp.newFile("tmpfile");
Data d = download("data.dat");
d.save(f);
assertThat(load(f), is(d));
}
}
25. TestWatcher
テストの前後に処理を挿入
before, after, around アドバイスっぽい感じ
24
public class TestWatcherTest {
@Rule
public TestWatcher tw = new TestWatcher() {
@Override
protected void failed(Description desc) {
// 失敗したときの処理
}
};
...
}