博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实验七——Web应用测试(bookstore项目上完成)
阅读量:4325 次
发布时间:2019-06-06

本文共 2546 字,大约阅读时间需要 8 分钟。

1、  编写单元测试用例,对用户注册功能的Action层进行测试。(注意:测试用例应考虑成功和失败的情况)

 

先在原来的UserAction类加入判断代码当用户名或密码为空时则注册失败:

public String register() throws Exception{

     if("" == user.getUsername()||""==user.getPassword()){

         return "error";

     }

     else{

     userService.saveUser(user);

     return SUCCESS;

     }

}

然后在UserAction类上创建一个测试类:

package org.easybooks.bookstore.action;

 

import org.easybooks.bookstore.service.IUserService;

import org.easybooks.bookstore.vo.User;

import org.junit.Test;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

/*

 * 用户注册(成功)

*  Actor:王燕红

 * */

 

public class UserActionTest {

@Test

public void testRegisterSuccess() throws Exception {

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

    

     User user=new User();

     user.setUsername("WYH");

     user.setPassword("123456");

     user.setSex("女");

     user.setAge(23);

    

     UserAction userAction=new UserAction();

     userAction.setUser(user);

     userAction.setUserService((IUserService)factory.getBean( "userService" ));

 

     String result=userAction.register();

     System.out.println("结果:"+user.getUsername()+"注册"+result);

}

   }

当用户和密码不为空时,则用户注册成功:

package org.easybooks.bookstore.action;

 

import org.easybooks.bookstore.service.IUserService;

import org.easybooks.bookstore.vo.User;

import org.junit.Test;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

/*

 * 用户注册(失败)

*  当密码或用户名为空时,则注册失败

*  Actor:王燕红

 * */

 

public class UserActionTest {

@Test

public void testRegisterSuccess() throws Exception {

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

       

        User user=new User();

        user.setUsername("WYH");

        user.setPassword("");

        user.setSex("女");

        user.setAge(23);

       

        UserAction userAction=new UserAction();

        userAction.setUser(user);

        userAction.setUserService((IUserService)factory.getBean( "userService" ));

 

        String result=userAction.register();

        System.out.println("结果:"+user.getUsername()+"注册"+result);

}

 

   }

2、  编写单元测试用例,对用户注册功能的DAO层进行测试。(注意:测试用例应考虑成功和失败的情况)

 

 

在TestUserDAO()中加入测试用例代码:

@Test

     public void testUserReg(){

         User user= new User();

         user.setUsername("王燕红");

         user.setSex("女");

         user.setPassword("123456");

         user.setAge(22);

         try {

             if(userDAO.exitUser(user.getUsername())){

                 System.out.println("注册失败,"+user.getUsername()+"用户名已经存在,请重新注册!!");

             }else{

                 userDAO.saveUser(user);

                 System.out.println("注册用户:"+user.getUsername()+"成功");

             }

        

         } catch (Exception e) {

             System.out.println("注册失败");

         }  

        

        }

若在重新运行一次则出现注册失败,提示重新注册!

 

转载于:https://www.cnblogs.com/cheerwyh/p/5577694.html

你可能感兴趣的文章
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
优化后的二次测试Miller_Rabin素性测试算法
查看>>
内部类。
查看>>
我的大学生活-4-21-吕家尧
查看>>
5、手把手教React Native实战之盒子模型BoxApp
查看>>
18日站立会议
查看>>
UIDynamic(物理仿真)
查看>>
AngularJS Scope(作用域)
查看>>
HttpClient相关
查看>>
DEPHI XE5 XE6 ANDROID IOS开发的几点体会
查看>>
angular.js 验证码注册登录
查看>>
团队站立会议08
查看>>
软件自动化测试学习步骤
查看>>
vector 简单使用
查看>>
20139216网络攻防技术第七次作业
查看>>
Sublime Text 配置
查看>>
【杂谈】需要mark的一些东西
查看>>