实验七——Web应用测试(bookstore项目上完成)

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("注册失败");

         }  

        

        }

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

 

posted @ 2016-06-12 15:23  不像样  阅读(323)  评论(0编辑  收藏  举报