package test.Junit; import Junit.Calculator; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.Collection; import static org.junit.Assert.assertEquals; /** * Created by spook on 16/5/18. */ @RunWith(value = Parameterized.class) public class ParameterizedTest { @Before public void before() throws Exception{ } private double expected; private double valueone; private double valuetwo; @Parameters public static Collection<Integer[]> getTestParameters(){ return Arrays.asList(new Integer[][]{ {2,1,1}, {3,2,1}, {5,3,1} }); } public ParameterizedTest(double expected,double valueone,double valuetwo){ this.expected=expected; this.valueone=valueone; this.valuetwo=valuetwo; } @Test public void sum(){ Calculator cal = new Calculator(); assertEquals(expected, cal.add(valueone, valuetwo),1);//第三个参数为误差,因此第三题case可以运行通过 } }
Junit参数化运行
1、Junit调用静态方法 getTestParameters,接着junit为getTestParameters集合中的每个数组进行循环;
2、然后Junit调用唯一的构造函数,如果存在多个构造函数会抛断言错误,接着用数组元素构成一系列参数调用构造函数{2,1,1}等
3、重复以上过程