Jubit用于对指定类当中的方法进行测试操作
建立一个学生管理类对象,然后对该类当中的方法使用Junit来进行测试操作
package com.hibernate.entity; /** * * @author Administrator *对学生信息进行管理操作 */ public class ManagerStudent { public void addStudent() { System.out.println("进行学生对象的添加操作"); } public void updateStudent() { System.out.println("对学生信息进行修改曹组"); } public String findStudent() { System.out.println("对学生信息进行查询曹组"); return "qingzhiyu"; } }
建立Junit类来对上述类当中的方法进行测试操作:
package com.hibernate.entity.test; import junit.framework.Assert; import junit.framework.TestCase; import com.hibernate.entity.ManagerStudent; /** * * @author Administrator *当前类继承了了TestCase可以用于实现测试操作 *用于对指定类当中的方法进行测试操作 */ public class TestManagerStudent extends TestCase{ public void testAdd() { System.out.println("对学生添加方法进行测试操作"); ManagerStudent managerStudent=new ManagerStudent(); managerStudent.addStudent(); managerStudent.updateStudent(); managerStudent.findStudent(); } public void testUpdate() { System.out.println("对学生信息修改进行测试"); ManagerStudent managerStudent=new ManagerStudent(); managerStudent.updateStudent(); } /** * 当对当前的方法进行执行之后会发现出现一个错误,即程序的预期值和实际的结果值是不相同的 */ public void testFind() { ManagerStudent managerStudent=new ManagerStudent(); System.out.println("对学生查找方法进行测试"); managerStudent.findStudent(); // 采用断言来判断测试程序最后所进行执行的结果是否是正确的 String actual=managerStudent.findStudent(); String expected="zhouxiaoqing"; // 采用断言语句来判断程序的实际结果和预期结构是否是相同一致的 Assert.assertEquals(expected, actual); } }
在测试类当中一共对三个类进行了测试,其中前两个方法进行测试之后发现可以正常进行执行,在testfind方法当中进行测试的时候使用了断言语句发现程序执行后实际的结果值和预期的结果值是不相同的,所以出现错误。
程序运行结果图为: