Home > Java > javaTutorial > New Java Movement: Test Driven Development 3---User Registration

New Java Movement: Test Driven Development 3---User Registration

黄舟
Release: 2016-12-30 13:11:24
Original
1241 people have browsed it

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

Let us start with a simple step. The first step is to write a test case, pass in cmd=registerUser, userName=Yan Tao, then call the Servlet, and finally in d:/ablog/app. The received user name is written back in the html file. The first name is the test code:

@Test
	public 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闫涛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 block
			e.printStackTrace();
		}
		assertTrue(1>0);
	}
Copy after login

The following is the code to pass this test case. First, when MainServlet jumps according to the command parameters, add the following code:

switch (cmd) {  
        case "registerUser":  
            registerUser(request, response);  
            break;
Copy after login

The specific processing function is as follows Display:

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 + "!");  
    }
Copy after login

Run the test case, then open d:/ablog/app.html, you will find that the user name has been printed into the file.

But wait a minute, the above test steps are not only not automated, but also more convenient than directly opening the browser to access the URL, especially when the returned page contains a lot of content. So we need to transform the above test cases so that they can be tested automatically. As you can know from the previous article, we will store the content that needs to be displayed on the page in the request object, usually in the form of Map. The page just gets the value from it, and then prints it to the page. Therefore, we can automatically determine whether our function development is correct by verifying the content stored in Map.

Okay, let’s implement this feature first. In MainServlet.registerUser, define the Map 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);
Copy after login

In the test case, we test whether the userId value is set correctly. The code is as follows Display:

Map<String, Object> model = (Map<String, Object>)request.getAttribute("model");  
            if (model.get("userId") != null && Long.parseLong("" + model.get("userId"))==101) {  
                rst = true;  
            }
Copy after login

At this time we will find that the test case cannot pass! This is normal, because the getAttribute and setAttribute methods are not implemented in our HttpJunitRequest object. In order 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>();  
  
@Override  
    public Object getAttribute(String key) {  
        return attributes.get(key);  
    }  
  
@Override  
    public void setAttribute(String key, Object value) {  
        attributes.put(key, value);  
    }
Copy after login

Run the test case again at this time, and finally you can It shows the green pass mark that makes us happy both physically and mentally.

So far, we have basically built a minimum runnable system and can develop it according to the concept of TDD.

As can be seen from the above example, we first think of a test case for a small function every time, and then code to try to pass this test case. After passing it, we continue to add new functions. Each test, development, and verification only takes up to 20 to 30 minutes. The code written in this way has basically been fully tested, and the code quality can be guaranteed to a certain extent.

The above is the content of the New Java Movement: Test Driven Development 3---user registration. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template