Resource > Junit
1. クラスのシグネチャーに @RunWith(Parameterized.class) を追加。
2. @Parametersを付与したメソッドを作成して、コンストラクタに渡す引数のリストを作成する。
コンストラクタでは受け取った引数をクラス変数にセットする。
Parameterized
テストケースをパラメータ化することで、記述量を削減できる。1. クラスのシグネチャーに @RunWith(Parameterized.class) を追加。
2. @Parametersを付与したメソッドを作成して、コンストラクタに渡す引数のリストを作成する。
コンストラクタでは受け取った引数をクラス変数にセットする。
@RunWith(Parameterized.class) public class FibonacciTest { @Parameters public static Collection<Object[]> data() { Object[][] obj = { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }; return Arrays.asList(obj); } private int fInput; private int fExpected; public FibonacciTest(int input, int expected) { fInput = input; fExpected = expected; } @Test public void test() { assertEquals(fExpected, Fibonacci.compute(fInput)); } public static class Fibonacci { public static int compute(int input) { if (input <= 1) return input; return compute(input - 2) + compute(input - 1); } } }