实验七 Web应用测试 _编写单元测试用例,对用户注册功能的Action层进行测试。
成功:
/*
* 用户注册(成功)
*/
@Test
public void testRegisterSuccess() throws Exception {
BeanFactoryfactory= new ClassPathXmlApplicationContext("applicationContext.xml");
User user=new User();
user.setUsername("ql");
user.setPassword("123");
user.setSex("女");
user.setAge(20);
UserAction userAction=new UserAction();
userAction.setUser(user);
userAction.setUserService((IUserService)factory.getBean( "userService" ));
String result=userAction.register();
System.out.println("注册结果:"+result);
}
失败:
/*
* 用户注册(失败)
* */
@Test
public void testRegisterFail() throws Exception {
BeanFactoryfactory= new ClassPathXmlApplicationContext("applicationContext.xml");
User user=new User();
user.setUsername("ql");
//user.setPassword("123");
user.setSex("女");
user.setAge(20);
UserAction userAction=new UserAction();
userAction.setUser(user);
userAction.setUserService((IUserService)factory.getBean( "userService" ));
try {
String result=userAction.register();
if(result=="sucess"){
System.out.println("成功");
}
else{
System.out.println("注册失败!!");
}
} catch (Exception e) {
System.out.println("注册失败");
}
}