一、Junit传参到单元测试的方式
在JUnit中,您可以通过以下方法将参数传递到单元测试方法:
构造函数
通过注解@Parameter注入
P.S 本教程测试使用Junit版本为4.12
二、多个参数的测试
创建类MatchUtils,里面有个简单的加法运算,代码如下:
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
}
三、Parameterized via Constructor
通过构造函数传递参数
ParameterizedTest.java
import com.mkyong.examples.MathUtils;
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;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(value = Parameterized.class)
public class ParameterizedTest {
pri