综合案例:登录与注册-基于TDD测试驱动开发

综合案例:登录与注册-基于TDD测试驱动开发
在这里插入图片描述
分析:
在这里插入图片描述
登陆失败带数据返回浏览器,登陆成功不带数据转到main

登录

基于TDD测试驱动开发,保证业务逻辑完全正确。在这个基础上才可能保证界面显示正常

1:编写测试

package com.rh.package05;

import com.rh.Service.Day05LoginUserSevice;
import com.rh.bean.UserDay05;
import org.junit.Test;

public class TestLoginUserService {
    @Test
    public void test01(){
        //1:获取参数
        String username="jack";
        String password="123";
        UserDay05 user=new UserDay05(username,password);
        //2:调用登录
        Day05LoginUserSevice userService=new Day05LoginUserSevice();
       //返回值Boolean只有两种情况,返回值为数字情况可能多一些
        int code=userService.ligon(user);
        System.out.print(code);


    }
}

2:编写逻辑

package com.rh.Service;

import com.rh.Dao.UserDaoday05;
import com.rh.bean.UserDay05;

//登陆与注册
public class Day05LoginUserSevice {
    public int ligon(UserDay05 user) {
        //查找是否存在账号密码
        UserDaoday05 dao=new UserDaoday05();
        int count=dao.find(user);
        return count;
    }
}
package com.rh.Dao;

import com.rh.bean.UserDay05;

import java.util.ArrayList;
import java.util.List;

public class UserDaoday05 {

    private static  List<UserDay05> userlist=new ArrayList<>();
    static{
        userlist.add(new UserDay05("jack","1234"));
        userlist.add(new UserDay05("rose","1234"));
    }
    public int find(UserDay05 user){
        for(UserDay05 u:userlist){
            //存在 账号与密码  相同的用户
            if(user.getUsername().equals(u.getUsername())&&user.getPassword().equals(u.getPassword())){
                return 1;
            }
        }
        return -1;
    }
}

package com.rh.bean;

public class UserDay05 {
private String username;
private String password;

public UserDay05() {
}

public UserDay05(String username, String password) {
    this.username = username;
    this.password = password;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@Override
public String toString() {
    return "UserDay05{" +
            "username='" + username + '\'' +
            ", password='" + password + '\'' +
            '}';
}

}

webUI
登陆页面,核心就是一个表单

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/13
  Time: 10:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post"  action="/myweb03/day05LoginServlet"><br/>
    username:<input type="text" name="username"><br/>
    password:<input type="text" name="password"><br/>
    <input type="submit" value="login"><br/>
</form>
</body>
</html>

配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <welcome-file-list>
      <welcome-file>Loginday05.jsp</welcome-file>
  </welcome-file-list>
</web-app>

web–loginservlet
编写页面请求的servlet

package com.rh.day05packageServlet;

import com.rh.Service.Day05LoginUserSevice;
import com.rh.bean.UserDay05;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/day05LoginServlet")
public class day05LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          //1:获取参数
          //1.5:设置请求体中的中文编码utf-8,解决输入的账号为中文名
        request.setCharacterEncoding("utf-8");
        Map<String,String[]> map=request.getParameterMap() ;
        UserDay05 user=new UserDay05();

        //1:创建WEB-INF/lib目录
        //1.2:添加beanUtils jar
        //1.3:绑定
        //1.4:populate方法调用
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        //2:调用登录
        Day05LoginUserSevice userService=new Day05LoginUserSevice();
        int code=userService.login(user);
        System.out.print(code);


    }
}

在这里插入图片描述
查看结果,登录之后的页面显示

怕不断是否输入正确,然后跳转另一个页面还是携带数据会登录1页面显示输入错误

package com.rh.day05packageServlet;

import com.rh.Service.Day05LoginUserSevice;
import com.rh.bean.UserDay05;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/day05LoginServlet")
public class day05LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          //1:获取参数
          //1.5:设置请求体中的中文编码utf-8,解决输入的账号为中文名
        request.setCharacterEncoding("utf-8");
        Map<String,String[]> map=request.getParameterMap() ;
        UserDay05 user=new UserDay05();

        //1:创建WEB-INF/lib目录
        //1.2:添加beanUtils jar
        //1.3:绑定
        //1.4:populate方法调用
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        //2:调用登录
        Day05LoginUserSevice userService=new Day05LoginUserSevice();
        int code=userService.login(user);
        System.out.print(code);
         if(code==1){
             //显示主页
             response.sendRedirect(request.getContextPath()+"/day05home.jsp");
         }else{
             //-1
             //回到登陆页面
             request.setAttribute("error_msg","账号或者密码错误");
             request.getRequestDispatcher("Loginday05.jsp").forward(request,response);
         }

    }
}

jsp:获取请求转发携带数据输出

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/13
  Time: 10:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<font color="red">${error_msg}</font>
<form method="post"  action="/myweb03/day05LoginServlet"><br/>
    username:<input type="text" name="username"><br/>
    password:<input type="text" name="password"><br/>
    <input type="submit" value="login"><br/>
</form>
</body>
</html>

在这里插入图片描述
注册
在这里插入图片描述
在这里插入图片描述
注册逻辑

注册test

package com.rh.package05;

import com.rh.Service.Day05LoginUserSevice;
import com.rh.bean.UserDay05;
import org.junit.Test;

public class TestLoginUserService {
    @Test
    public void test01(){
        //1:获取参数
        String username="jack";
        String password="123";
        UserDay05 user=new UserDay05(username,password);
        //2:调用登录
        Day05LoginUserSevice userService=new Day05LoginUserSevice();
       //返回值Boolean只有两种情况,返回值为数字情况可能多一些
        int code=userService.login(user);
        System.out.print(code);

    }

    @Test
    public void test02(){
      String username="jack";
      String password="1234";
      UserDay05 user=new UserDay05(username,password);

      Day05LoginUserSevice userSevice=new Day05LoginUserSevice();
      int code=userSevice.register(user);
      System.out.println("service"+code);



    }
}

注册的service

package com.rh.Service;

import com.rh.Dao.UserDaoday05;
import com.rh.bean.UserDay05;

//登陆与注册
public class Day05LoginUserSevice {

    public int login(UserDay05 user) {
        //查找是否存在账号密码
        UserDaoday05 dao=new UserDaoday05();
        int count=dao.find(user);
        return count;
    }

    public int register(UserDay05 user) {
         //查找用户名是否已存在,不存在进行保存,否则不保存提示失败
        UserDaoday05 dao=new UserDaoday05();
        int count=dao.findByName(user.getUsername());
        if(count==0){
            //不存在,进行保存
            dao.add(user);
            System.out.println("Service保存");
             return 1;
        }else{
            System.out.println("Service不保存");
            return -1;

        }
    }
}

注册dao

package com.rh.Dao;

import com.rh.bean.UserDay05;

import java.util.ArrayList;
import java.util.List;

public class UserDaoday05 {

    private static  List<UserDay05> userlist=new ArrayList<>();
    static{
        userlist.add(new UserDay05("jack","1234"));
        userlist.add(new UserDay05("rose","1234"));
    }
    public int find(UserDay05 user){
        for(UserDay05 u:userlist){
            //存在 账号与密码  相同的用户
            if(user.getUsername().equals(u.getUsername())&&user.getPassword().equals(u.getPassword())){
                return 1;
            }
        }
        return -1;
    }

    public int findByName(String username) {
        for(UserDay05 u:userlist){
            //存在 账号与密码  相同的用户
            if(u.getUsername().equals(username)){
                System.out.println("Dao找到");
                return 1;
            }
        }
        System.out.println("Dao没找到");
        return -1;
    }

    public void add(UserDay05 user) {
        System.out.println("Dao保存");
        userlist.add(user);
    }
}

注册用户

package com.rh.bean;

public class UserDay05 {
    private String username;
    private String password;
    private String password2;
    private String email;
    private String name;
    private String gender;
    private String birthday;

    public UserDay05() {
    }

    public UserDay05(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public UserDay05(String username, String password, String password2, String email, String name, String gender, String birthday) {
        this.username = username;
        this.password = password;
        this.password2 = password2;
        this.email = email;
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserDay05{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", passsword2='" + password2 + '\'' +
                ", email='" + email + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday='" + birthday + '\'' +
                '}';
    }
}

实现代码:

注册页面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/13
  Time: 10:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<font color="red">${error_msg}</font>
<form method="post"  action="/myweb03/day05RegisterServlet"><br/>
<%--    //name一定要与user成员变量相对应,反射--%>
    用户名:<input type="text" name="username"><br/>
    密码:<input type="text" name="password"><br/>
    确认密码:<input type="text" name="password2"><br/>
   email:<input type="text" name="email"><br/>
    姓名:<input type="text" name="name"><br/>
    性别:<input type="radio" name="gender" value="1"><input type="radio" name="geder" value="2"><br/>
    出生日期:<input type="date" name="birthday" >

    <input type="submit" value="login"><br/>
</form>
</body>
</html>

注册servlet

package com.rh.day05packageServlet;

import com.rh.Service.Day05LoginRegisterUserSevice;
import com.rh.bean.UserDay05;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/day05RegisterServlet")
public class day05RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1:获取参数
        //1.5:设置请求体中的中文编码utf-8,解决输入的账号为中文名
        request.setCharacterEncoding("utf-8");
        Map<String,String[]> map=request.getParameterMap() ;
        UserDay05 user=new UserDay05();

        //1:创建WEB-INF/lib目录
        //1.2:添加beanUtils jar
        //1.3:绑定
        //1.4:populate方法调用
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        //2:调用注册
        Day05LoginRegisterUserSevice userService=new Day05LoginRegisterUserSevice();
        int code=userService.register(user);

        //如果用户注册成功,回到登陆页面取登录
        //如果注册失败,回到注册页面重新注册
        if(code==1){
            response.sendRedirect(request.getContextPath()+"/Loginday05.jsp");
    }else{
            request.setAttribute("error_msg","该用户名已被注册");
            request.getRequestDispatcher("Registerday05.jsp").forward(request,response);
        }
    }
}

原理
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
测试驱动的编程是 XP 困扰程序员的一个方面。对于测试驱动的编程意味着什么以及如何去做,大多数人都做出了不正确的假设。这个月,XP 方面的讲师兼 Java 开发人员 Roy Miller 谈论了测试驱动的编程是什么,它为什么可以使程序员的生产力和质量发生巨大变化,以及编写测试的原理。请在与本文相随的 论坛中提出您就本文的想法,以飨笔者和其他读者。(您也可以单击本文顶部或底部的“讨论”来访问该论坛。) 最近 50 年来,测试一直被视为项目结束时要做的事。当然,可以在项目进行之中结合测试测试通常并不是在 所有编码工作结束后才开始,而是一般在稍后阶段进行测试。然而,XP 的提倡者建议完全逆转这个模型。作为一名程序员,应该在编写代码 之前编写测试,然后只编写足以让测试通过的代码即可。这样做将有助于使您的系统尽可能的简单。 先编写测试 XP 涉及两种测试: 程序员测试和 客户测试测试驱动的编程(也称为 测试为先编程)最常指第一种测试,至少我使用这个术语时是这样。测试驱动的编程是让 程序员测试(即单元测试 ― 重申一下,只是换用一个术语)决定您所编写的代码。这意味着您必须在编写代码之前进行测试测试指出您 需要编写的代码,从而也 决定了您要编写的代码。您只需编写足够通过测试的代码即可 ― 不用多,也不用少。XP 规则很简单:如果不进行程序员测试,则您不知道要编写什么代码,所以您不会去编写任何代码。 测试驱动开发(TDD)是极限编程的重要特点,它以不断的测试推动代码的开发,既简化了代码,又保证了软件质量。本文从开发人员使用的角度,介绍了 TDD 优势、原理、过程、原则、测试技术、Tips 等方面。 背景 一个高效的软件开发过程对软件开发人员来说是至关重要的,决定着开发是痛苦的挣扎,还是不断进步的喜悦。国人对软件蓝领的不屑,对繁琐冗长的传统开发过程的不耐,使大多数开发人员无所适从。最近兴起的一些软件开发过程相关的技术,提供一些比较高效、实用的软件过程开发方法。其中比较基础、关键的一个技术就是测试驱动开发(Test-Driven Development)。虽然TDD光大于极限编程,但测试驱动开发完全可以单独应用。下面就从开发人员使用的角度进行介绍,使开发人员用最少的代价尽快理解、掌握、应用这种技术。下面分优势,原理,过程,原则,测试技术,Tips等方面进行讨论。 1. 优势 TDD的基本思路就是通过测试来推动整个开发的进行。而测试驱动开发技术并不只是单纯的测试工作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值