New Java movement: Test-driven development 3-User Registration

Source: Internet
Author: User

After the efforts in the previous section, we can finally enter the formal function development.

The user registration process is to enter the user name and password, and then correctly establish the user's basic information and account information to the database.

Let's start with a simple step. The first step is to write a test case, pass in cmd = registeruser, username = Yan Tao, then call servlet, and finally at D: write back the username received in the/ablog/app.html file. First name is a tryCode:

 
@ Testpublic void testregisteruser001 () {httpservletrequest request = new httpjunitrequest (); Map <string, string []> Params = (Map <string, string []>) request. getparametermap (); string [] cmd = new string [1]; cmd [0] = "registeruser"; Params. put ("cmd", CMD); string [] username = new string [1]; username [0] = "y Yan Tao t"; Params. put ("username", username); mainservlet M = new mainservlet (); httpservletresponse response = new httpjunitresponse (); try {M. doget (request, response); response. getwriter (). close ();} catch (ioexception | servletexception e) {// todo auto-generated catch blocke. printstacktrace ();} asserttrue (1> 0 );}

The following is a test case by writing code. First, add the following code to the mainservlet jump Based on command parameters:

 
Switch (CMD) {Case "registeruser": registeruser (request, response); break;

The specific processing functions are as follows:

 
Private void registeruser (httpservletrequest request, httpservletresponse response) throws ioexception, servletexception {printwriter out = response. getwriter (); string username = NULL; If (request. getparameter ("username ")! = NULL) {username = request. getparameter ("username");} Out. Print ("username =" + username + "! ");}

Run the test case and open D:/ablog/app.html. the user name is printed in the file.

But wait a moment, the above test step aggregation is not only not automated, but also more convenient than directly opening a browser to access the URL, especially when a large amount of page content is returned. Therefore, we need to transform the above test cases so that they can be automated. From the frontArticleWe can know that the content to be displayed on the page is stored in the request object, which is usually stored in the form of Map <string, Object>. The page only obtains the value from it, then print it to the page, so we can automatically determine whether our function development is correct by verifying the content stored in Map <string, Object>.

Now let's implement this feature first. In mainservlet. registeruser, define the Map <string, Object> model and add it to the request. The Code is as follows:

 
Map <string, Object> model = new hashmap <string, Object> (); long userid = 101; model. put ("userid", "" + userid); Request. setattribute ("model", model );

In the test case, we test whether the userid value is set correctly. The Code is as follows:

 
Map <string, Object> model = (Map <string, Object>) request. getattribute ("model"); If (model. Get ("userid ")! = NULL & long. parselong ("" + model. Get ("userid") = 101) {rst = true ;}

At this time, we will find that the test case cannot pass! This is normal, because our httpjunitrequest object does not implement the getattribute and setattribute methods. To make the test case pass, we need to add the following code to httpjunitrequest:

 
Private final map <string, Object> attributes = new hashmap <string, Object> (); @ overridepublic object getattribute (string key) {return attributes. get (key) ;}@ overridepublic void setattribute (string key, object Value) {attributes. put (Key, value );}

Then we run the test case to display the green pass sign that makes us physically and mentally happy.

So far, we have basically built a runable minimal system and can develop it according to the TDD concept.

As shown in the above example, every time we first think about a test case for a small function, then code tries to pass this test case, and after passing the test case, we will continue to add new features. Each test, Development, and verification takes up to two or thirty minutes. The code written in this way has basically been fully tested, and the code quality can be guaranteed to a certain extent.

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.