实验七 Web应用测试_编写单元测试用例,对用户注册功能的DAO层进行测试
package testRegister;
import java.util.HashSet;
import junit.framework.Assert;
import org.easybooks.bookstore.dao.IUserDAO;
import org.easybooks.bookstore.vo.User;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
public class TestUserDAO extends AbstractDependencyInjectionSpringContextTests{
//私有变量的Set方法来注入你想要的来自applicationContext中的bean,而不需要显式的调用applicationContext.getBean(XXX)
//该类会从getConfigLocations()方法指定的配置文件中帮你自动注入
private IUserDAO userDAO;
public void setUserDAO(IUserDAO _userDAO){
userDAO=_userDAO;
}
@Override
protected String[] getConfigLocations() {
//指定Spring配置文件applicationContext.xml
return new String[] {"classpath:org/easybooks/bookstore/test/applicationContext.xml"};
}
成功:
/**
* 测试UserDAO中的register()方法——注册成功
*/
public void testRegisterSuccess(){
User user = new User("ql","ql","woman",20,new HashSet(0));
boolean isexit = userDAO.exitUser("ql");
Assert.assertFalse(isexit);
userDAO.saveUser(user);
System.out.println("注册成功,用户为:"+user.getUsername());
}
失败:
/**
* 测试UserDAO中的register()方法——注册失败
*/
public void testRegisterFail(){
boolean isexit = userDAO.exitUser("zxc");
Assert.assertTrue(isexit);
System.out.println("注册失败");
}
}