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.)

Source: Internet
Author: User

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.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.