JUnitParams(github地址:https://github.com/Pragmatists/JUnitParams)作为一个开源的单元测试框架,提供了参数化测试,Coder不需要通过构造器来设置参数,JUnitParams可以由测试方法提供参数,减少了代码量。接下来咱们就一探究竟。
<dependencies>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
注释:不使用maven的用户可以在maven仓库中下载jar包
1.2 Student.java
/**
* Created by zhangzh on 2017/2/20.
*/
public class Student {
private String name;
private String studentNo;
private int age;
private String schoolName;
public Student(int age) {
this.age = age;
}
public boolean isHighStudent() {
return age >= 15 && age < 25;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", studentNo='" + studentNo + '\'' +
", age=" + age +
", schoolName='" + schoolName + '\'' +
'}';
}
}
1.3 测试类:JUnitParamsTest.java
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Created by zhangzh on 2017/2/23.
*/
@RunWith(JUnitParamsRunner.class)
public class JUnitParamsTest {
@Test
@Parameters({
"3, false",
"20, true",
"29, false"})
public void testHighStudent(int age, boolean valid) {
System.out.println("age=" + age +",valid=" + valid);
Assert.assertEquals(new Student(age).isHighStudent(), valid);
}
@Test
@Parameters(method = "ageValues")
public void studentIsHighStudent(int age, boolean valid) throws Exception {
System.out.println("age=" + age +",valid=" + valid);
Assert.assertEquals(new Student(age).isHighStudent(), valid);
}
private Object[] ageValues() {
return new Object[]{
new Object[]{13, false},
new Object[]{17, true},
new Object[]{35, false}
};
}
/**
* 默认约定,当不指定@Parameters中的method方法时,JUnitParams默认查找“parametersFor + 自己做注解的方法名称”作为@Parameters的参数方法
* 例如,@Parameters注解方法为:personIsAdult_2,则其默认关联的方法名称为:parametersForPersonIsAdult_2
*/
@Test
@Parameters
public void studentIsHighStudent_2(int age, boolean valid) throws Exception {
System.out.println("age=" + age +",valid=" + valid);
Assert.assertEquals(new Student(age).isHighStudent(), valid);
}
private Object[] parametersForStudentIsHighStudent_2() {
return new Object[]{
new Object[]{13, false},
new Object[]{17, true},
new Object[]{36, false}
};
}
/**
* 变量传入对象
*/
@Test
@Parameters
public void studentIsHighStudent_3(Student student, boolean valid) throws Exception {
System.out.println("age=" + student.getAge() +",valid=" + valid);
Assert.assertEquals(student.isHighStudent(), valid);
}
private Object[] parametersForStudentIsHighStudent_3() {
return new Object[]{
new Object[]{new Student(13), false},
new Object[]{new Student(17), true},
new Object[]{new Student(45), false}
};
}
}
看,简单吧!
参考资料:1.JunitParams的github地址