Java分层设计(DAO设计模式)—JUnit测试

来源:互联网 发布:知乎达芬奇的恶魔 编辑:程序博客网 时间:2024/06/07 12:40

本篇用于对Java分层设计(DAO设计模式)的测试而写。

一、选中IDeptService右键-NEW-JUnit Test Case


创建在cn.mldn.test.junit包下


二、点击next,选中全部



三、点击OK,选中导入JUnit包



四、添加测试代码

package cn.mldn.test.junit;import static org.junit.Assert.fail;import java.util.HashSet;import java.util.Set;import org.junit.Test;import cn.mldn.factory.ServiceFactory;import cn.mldn.vo.Dept;import junit.framework.TestCase;public class IDeptServiceTest {@Testpublic void testInsert() {Dept vo = new Dept() ;vo.setDeptno(11);vo.setDname("教育");vo.setLoc("北京");try {TestCase.assertTrue(ServiceFactory.getIDeptServiceInstance().insert(vo));} catch (Exception e) {e.printStackTrace();}}@Testpublic void testUpdate() {Dept vo = new Dept() ;vo.setDeptno(11);vo.setDname("体育");vo.setLoc("天津");try {TestCase.assertTrue(ServiceFactory.getIDeptServiceInstance().update(vo));} catch (Exception e) {e.printStackTrace();}}@Testpublic void testDelete() {Set<Integer> ids = new HashSet<Integer>() ;ids.add(11) ;try {TestCase.assertTrue(ServiceFactory.getIDeptServiceInstance().delete(ids));} catch (Exception e) {e.printStackTrace();}}@Testpublic void testList() {try {TestCase.assertTrue(ServiceFactory.getIDeptServiceInstance().list().size() > 0);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic void testGet() {try {TestCase.assertNotNull(ServiceFactory.getIDeptServiceInstance().get(10));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


五、点击Window—>show view—>outline,选中IDeptServiceTest类进行全部测试,或者逐一选中类中的方法,进行分别测试。只需选中,右键,Run as—>JUnit Test



六、测试结果


显示绿条,则表明测试结果正确。

查看数据库:


成功添加。

其他方法就不逐一演示了。


IEmpService的测试,操作步骤同上。

IEmpServiceTest代码如下:
package cn.mldn.test.junit;import static org.junit.Assert.*;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.junit.Test;import cn.mldn.factory.ServiceFactory;import cn.mldn.vo.Emp;import junit.framework.TestCase;public class IEmpServiceTest {@Testpublic void testInsert() {Emp vo = new Emp() ;vo.setEmpno(8899);vo.setEname("程光友");vo.setJob("摄影师");vo.setHiredate(new Date());vo.setSal(10.0);vo.setComm(0.5);try {TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().insert(vo));} catch (Exception e) {e.printStackTrace();}}@Testpublic void testUpdate() {Emp vo = new Emp() ;vo.setEmpno(8899);vo.setEname("陈冠佐");vo.setJob("演员");vo.setHiredate(new Date());vo.setSal(10.0);vo.setComm(0.5);try {TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().update(vo));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic void testDelete() {Set<Integer> ids = new HashSet<Integer>();ids.add(8899);try {TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().delete(ids));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic void testGet() {try {TestCase.assertNotNull(ServiceFactory.getIEmpServiceInstance().get(7369));} catch (Exception e) {e.printStackTrace();}}@Testpublic void testList() {try {TestCase.assertNotNull(ServiceFactory.getIEmpServiceInstance().list().size() > 0);} catch (Exception e) {e.printStackTrace();}}@Testpublic void testListIntIntStringString() {try {Map<String,Object> map = ServiceFactory.getIEmpServiceInstance().list(2,5,"ename","") ;//取出分页过后的记录条数,需要向下转型为Integerint count = (Integer)map.get("empCount") ;List<Emp> all = (List<Emp>) map.get("allEmps") ;TestCase.assertTrue(count > 0 && all.size() > 0);} catch (Exception e) {e.printStackTrace();}}}


原创粉丝点击