为了简化类似的测试,JUnit4提出了“参数化测试”的概念,只写一个测试函数,把这若干种情况作为参数传递进去,一次性的完成测试。代码如下:
import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays; import java.util.Collection;
@RunWith(Parameterized.class) public class SquareTest ...{
private static Calculator calculator = new Calculator(); private int param; private int result;
@Parameters public static Collection data() ...{ return Arrays.asList(new Object[][]...{ ...{2, 4}, ...{0, 0}, ...{-3, 9}, }); }
//构造函数,对变量进行初始化 |
推荐阅读: