我如何为Fibonacci系列编写Junit测试用例,使用参数化测试?
public static int fibonnacci(int number) {
if (number == 1 || number == 2) {
return 1;
}
return fibonnacci(number - 1) + fibonnacci(number - 2);
答案 0 :(得分:2)
尝试简单的测试,如:
public class MyClassTest {
MyClass clazz;
@Before
public void setUp() throws Exception {
clazz = new MyClass();
}
@Test
public void testFibbonacciWithOneAsInput() {//write different test cases and test for edge cases, normal cases something like below.
Assert.assertEquals(clazz.fibonnacci(1), 1);
}
}
答案 1 :(得分:1)
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },{ 6, 8 }
});
}
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));
}
}