单元测试
1.IDEA下载Junit插件
2.选定代码自动生成代码
3.写入测试语句测试功能执行效果
代码示例:
public class UserDaoTest { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void login() { UserDao dao = new UserDaoImpl(); RootUser rootUser = new RootUser(); rootUser.setId(1); rootUser.setUserid("123"); rootUser.setPassword("123"); rootUser.setStatus("0"); Assert.assertEquals("0",dao.login("123","123")); System.out.println("登录用户:"+rootUser.getUserid()); } @Test public void registerstu() { UserDao dao = new UserDaoImpl(); Student student = new Student(); for(int i = 0;i<10;i++){ student.setUsername("123"+i); student.setPassword("123"+i); student.setName("x"+i); student.setSex("男"); student.setBanji("1"+i); student.setNianji("1"+i); student.setStatus("1"); Assert.assertTrue(dao.registerstu(student)); System.out.println("添加username:"+student.getUsername()); } } @Test public void getStu() { UserDao dao = new UserDaoImpl(); Student student = new Student(); List<Student> list = new ArrayList<Student>(); Assert.assertNotNull(dao.getStu()); list = dao.getStu(); for (Student student1 : list) { System.out.println(student1); } } @Test public void deletes() { UserDao dao = new UserDaoImpl(); for(int i = 50;i<60;i++) { Assert.assertTrue(dao.deletes(1+i)); int j = i+1; System.out.println("删除id:"+j); } } @Test public void updatestu() { UserDao dao = new UserDaoImpl(); for(int i = 50;i<60;i++){ Assert.assertTrue(dao.updatestu(1+i,"321"+i,"321"+i,"xxx+i","女","2"+i,"2"+i)); int j = i+1; System.out.println("更新id:"+j); } } }