当你测试一个方法时,它的分支很多,要不又很多if else 或者 switch 等等,在这种情况下,要想写个case将其覆盖,需要写很多test,这些test基本内容都是一样的,唯一的不一样就是参数和期望参数,这时很麻烦,虽然可以vc大法,但是测试数据看起来也并不是一目了然,那么我今天就把我刚学到得DDT(Data Driver Test)注意,不是TDD(Test Drivern Develop)测试记录一下,声明,我是菜鸟,很菜。
下面是所要用到得bean:
package ddttest; import java.util.List; public class Person { private String name; private int age; private double salary; //有点递归的感觉 private List<Person> friends; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public List<Person> getFriends() { return friends; } public void setFriends(List<Person> friends) { this.friends = friends; } }
下面这个是要测试的类
package ddttest; public class XMLParameterizedAdvanced { public String comparePerson(Person person) { if(person.getSalary() > 8000 && (person.getAge() < 30)) { return "Good"; }else if(person.getSalary() > 20000 && !(person.getFriends().isEmpty())) { return "Nice"; }else { return "Normal"; } } }
下面是测试类,为了方便我直接就在ddttest下生成了:
package ddttest; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.junitext.XMLParameters; import org.junitext.runners.XMLParameterizedRunner; @RunWith(XMLParameterizedRunner.class) public class XMLParameterizedAdvancedTest { private Person expectedPerson; private String actualReturn; @XMLParameters("/ddttest/test.xml") public XMLParameterizedAdvancedTest(Person expectedPerson, String actualReturn){ this.expectedPerson = expectedPerson; this.actualReturn = actualReturn; } @Test public void testComparePerson() { XMLParameterizedAdvanced test = new XMLParameterizedAdvanced(); assertEquals(actualReturn,test.comparePerson(expectedPerson)); } }
最后是测试数据的XML文件:
<?xml version="1.0" encoding="ISO-8859-1" ?> <tests> <test> <bean id= "expectedPerson" class="ddttest.Person"> <property name = "name" value = "ChenHong"/> <property name = "age" value = "27"/> <property name = "salary" value = "9000"/> <property name = "friends" /> </bean> <string id = "actualReturn" value ="Good"/> </test> <test> <bean id= "expectedPerson" class="ddttest.Person"> <property name = "name" value = "Lin dan"/> <property name = "age" value = "30"/> <property name = "salary" value = "25000"/> <property name = "friends"> <list> <bean class="ddttest.Person"> <property name = "name" value = "Leecongwei"/> <property name = "age" value = "40"/> <property name = "salary" value = "5000"/> <property name = "friends" /> </bean> <bean class="ddttest.Person"> <property name = "name" value = "Jonason"/> <property name = "age" value = "40"/> <property name = "salary" value = "5000"/> <property name = "friends" /> </bean> </list> </property> </bean> <string id = "actualReturn" value ="Nice"/> </test> <test> <bean id= "expectedPerson" class="ddttest.Person"> <property name = "name" value = "Gade"/> <property name = "age" value = "27"/> <property name = "salary" value = "5000"/> <property name = "friends" /> </bean> <string id = "actualReturn" value ="Normal"/> </test> </tests>
再导入相应的jar包,就可以跑了,如果安装了,emma插件的,会发现覆盖率是百分之百。
注意:在编写测试数据的时候,指定class时,务必小心,不要将空格加入,这样会报
java.lang.ClassNotFoundException。。。
如果报了,那么一定是测试数据的XML没有正确,可以采用排除法,一点一点的定位出错误所在地点。
附件是一些jar包,其中jmockit.jar 是不用加的,加进去了可能会有问题。
还有,junit的src包也不要加,如果加了会出现异常。总之,自己多导两次包就应该可以找出问题了。
具体原因可能是junit的src包和junit的jar同时存在时,会有冲突。