1、编写单元测试用例,对用户注册功能的Action层进行测试。(注意:测试用例应考虑成功和失败的情况)
  先在原来的UserAction类加入判断代码当用户名或密码为空时则注册失败:
public String register() throws Exception{
if("" == user.getUsername()||""==user.getPassword()){
return "error";
}
else{
userService.saveUser(user);
return SUCCESS;
}
}
  然后在UserAction类上创建一个测试类:
package org.easybooks.bookstore.action;
import org.easybooks.bookstore.service.IUserService;
import org.easybooks.bookstore.vo.User;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* 用户注册(成功)
*  Actor:王燕红
* */
public class UserActionTest {
@Test
public void testRegisterSuccess() throws Exception {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
User user=new User();
user.setUsername("WYH");
user.setPassword("123456");
user.setSex("女");
user.setAge(23);
UserAction userAction=new UserAction();
userAction.setUser(user);
userAction.setUserService((IUserService)factory.getBean( "userService" ));
String result=userAction.register();
System.out.println("结果:"+user.getUsername()+"注册"+result);
}
}
  当用户和密码不为空时,则用户注册成功: