testcartview.java~95~

来自「购物车模块实例。购物车模块可以浏览商品类别;可以根据商品类别浏览商品信息;可以购」· JAVA~95~ 代码 · 共 67 行

JAVA~95~
67 行
字号
package cartservlet;

import org.apache.cactus.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import data.*;

public class TestCartView extends ServletTestCase {
  private CartView cartView = null;

  public TestCartView(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    super.setUp();
    cartView = new CartView();
    //初始化ServletContext对象
    cartView.init(config);
  }

  protected void tearDown() throws Exception {
    cartView = null;
    super.tearDown();
  }

  //为doPost1方法传入参数的方法
  public void beginDoPost1(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "商品浏览窗口", WebRequest.POST_METHOD);
    theRequest.addParameter("name", "", WebRequest.POST_METHOD);
  }
  //测试是否可以正确处理顾客名字为空的登陆
  public void testDoPost1() throws ServletException, IOException {
    request.setCharacterEncoding("GBK");
    //测试是否正确设置action参数和name参数
    assertEquals("商品浏览窗口", request.getParameter("action"));
    assertEquals("", request.getParameter("name"));
    //初始化ServletContext对象
    cartView.init(config);
    cartView.doPost(request, response);
    this.assertNotNull(request.getAttribute("infor"));
    //不能登陆,购物车对象应该为空
    this.assertNull(session.getAttribute("cart"));
  }
  //为doPost2方法传入参数的方法
  public void beginDoPost2(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "商品浏览窗口", WebRequest.POST_METHOD);
    theRequest.addParameter("name", "jack", WebRequest.POST_METHOD);
  }
  //测试是否可以正确处理顾客名字不为空的登陆
  public void testDoPost2() throws ServletException, IOException {
    request.setCharacterEncoding("GBK");
    //测试是否正确设置action参数和name参数
    assertEquals("商品浏览窗口", request.getParameter("action"));
    assertEquals("jack", request.getParameter("name"));
    cartView.doPost(request, response);
    //正确登陆,购物车对象不为空
    this.assertNotNull(session.getAttribute("cart"));
    Cart cart = (Cart)session.getAttribute("cart");
    this.assertEquals("顾客名字应该是jack", "jack", cart.getCustomer());
    System.out.print(cart.getCustomer());
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?