First of all, I would like to explain that the user registration action in the method does not make any judgment can be registered, nothing to consider, even if all is empty, he can register successfully. If you want to write a test case, follow my steps,
useraction In The register () method to add some judgment, and in the JSP page to give the corresponding hint, because there are too many loopholes, I wrote only one judgment-if the user name is a space, the registration failed. There are many test cases, and there are a lot of bugs that can be tested, I give an example:
In the first step, in the action code, add a judge to the Register () method:
User registration, calling the service layer's Saveuser () method
If username is a space when registering, the error is returned.
Public String Register () throws exception{
if ("" = = User.getusername ()) {
return "Error";
}Else{
Userservice.saveuser (user);
return SUCCESS;
}
}
Step Two: Create a new test class in the test class: I take the name testregisteraction.
I first wrote a successful registration method, and designed a successful test case, as follows:
@Test
Public void testregistersuccess () throws Exception {
Beanfactory factory = new Filesystemxmlapplicationcontext ("File:c:/documents and Settings/administrator/workspaces /myeclipse 10/bookstore/src/applicationcontext.xml ");
Beanfactory factory = new classpathxmlapplicationcontext ("Applicationcontext.xml");
User user=new user ();
User.setusername ("Roven");
User.setpassword ("123");
User.setsex ("male");
User.setage (23);
Useraction useraction=New useraction ();
Useraction.setuser (user);
Useraction.setuserservice ((Iuserservice) Factory.getbean ("UserService"));
String Result=useraction.register ();
System. out. println ("Result:" +user.getusername () + "register" +result);
}
The MyEclipse console results are as follows:
The results in the database are as follows:
Then I wrote a failed registration method and wrote a test case with a user name of a space:
MyEclipse The results are as follows:
The data does not have a user named space user, note that I said the space is a real space, the result is as follows:
The following topics are completed on the bookstore project (please run the database file first): 1, write unit test cases, test the action layer of user registration function. (Note: Test cases should consider success and failure.)