传统的的开发流程:
编码--->测试--->重复--->提交
基于测试驱动的开发流程:
测试--->编码--->重复--->提交
下面是一个简单的用户管理的用测试驱动开发的小小例子:
1、先定义好实体类User:
package com.fjnu.model; public class User { private String username; private String password; private String nickname; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, String nickname) { super(); this.username = username; this.password = password; this.nickname = nickname; } }
2、然后定义好数据操作层的接口IUserService:
package com.fjnu.service; import com.fjnu.model.User; public interface IUserService { public void add(User user); public void delete(String username); public User load(String username); public User login(String username, String password); }
4、有了这些,就可以针对接口IUserService编写测试类TestUserService了,这个测试类类似于白盒测试,要把各种分支的可能情况都要考虑到并且编写测试代码:
package com.fjnu.service; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import com.fjnu.model.User; import com.fjnu.model.UserException; public class TestUserService { private IUserService us; private User baseUser; @Before public void setUp(){ // 初始化 us = new UserServiceByMap(); baseUser = new User("admin", "123", "管理员"); } private void assertUserEquals(User u, User tu){ assertEquals("add方法有错误!", u.getUsername(), tu.getUsername()); assertEquals("add方法有错误!", u.getNickname(), tu.getNickname()); assertEquals("add方法有错误!", u.getPassword(), tu.getPassword()); } @Test public void testAdd(){ User u = baseUser; us.add(u); User tu = us.load("admin"); assertNotNull(tu); assertUserEquals(u, tu); //fail("请加入添加的测试代码"); } @Test(expected=UserException.class) public void AddExistUsername(){ us.add(baseUser); User tu = new User("admin", "1234", "alskdf"); us.add(tu); } @Test public void testDelete(){ us.add(baseUser); User tu = us.load(baseUser.getUsername()); assertNotNull(tu); us.delete(baseUser.getUsername()); tu = us.load(baseUser.getUsername()); assertNull(tu); } @Test public void testLogin(){ us.add(baseUser); String username=baseUser.getUsername(); String password=baseUser.getPassword(); User tu = us.login(username, password); assertUserEquals(baseUser, tu); } @Test(expected=UserException.class) public void testNotExistsUserLogin(){ us.add(baseUser); String username="admin1"; String password="123"; us.login(username, password); } @Test(expected=UserException.class) public void testPasswordErrorUserLogin(){ us.add(baseUser); String username="admin"; String password="1235"; us.login(username, password); } }
5、编写完测试类之后,就可以实现接口IUserService了:
package com.fjnu.service; import java.util.HashMap; import java.util.Map; import com.fjnu.model.User; import com.fjnu.model.UserException; public class UserServiceByMap implements IUserService { private Map<String, User> us = new HashMap<String, User>(); @Override public void add(User user) { // TODO Auto-generated method stub if(load(user.getUsername()) != null){ throw new UserException("用户名已存在"); } us.put(user.getUsername(), user); } @Override public void delete(String username) { // TODO Auto-generated method stub us.remove(username); } @Override public User load(String username) { // TODO Auto-generated method stub return us.get(username); } @Override public User login(String username, String password) { // TODO Auto-generated method stub User u = load(username); if(u==null) throw new UserException("用户名不存在"); if(!u.getPassword().equals(password)) throw new UserException("用户密码不正确"); return u; } }