⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testgoodsview.java~70~

📁 购物车模块实例。购物车模块可以浏览商品类别;可以根据商品类别浏览商品信息;可以购买商品;可以查看购物车的商品;可以修改购买商品的数量和删除购买的商品。
💻 JAVA~70~
字号:
package cactustest;

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

public class TestGoodsView extends ServletTestCase {
  private GoodsView goodsView = null;

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

  protected void setUp() throws Exception {
    super.setUp();
    goodsView = new GoodsView();
    //初始化ServletContext对象
    goodsView.init(config);
    request.setCharacterEncoding("GBK");
  }

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

  //为doPost方法传入参数的方法
  public void beginLoginPost1(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "商品浏览窗口", WebRequest.POST_METHOD);
    theRequest.addParameter("name", "", WebRequest.POST_METHOD);
  }
  //测试是否可以正确处理顾客名字为空的登陆
  public void testLoginPost1() throws ServletException, IOException {
    //测试是否正确设置action参数和name参数
    assertEquals("商品浏览窗口", request.getParameter("action"));
    assertEquals("", request.getParameter("name"));
    goodsView.doPost(request, response);
    this.assertNotNull(request.getAttribute("infor"));
    //不能登陆,购物车对象应该为空
    this.assertNull(session.getAttribute("cart"));
  }
  //为doPost方法传入参数的方法
  public void beginLoginPost2(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "商品浏览窗口", WebRequest.POST_METHOD);
    theRequest.addParameter("name", "jack", WebRequest.POST_METHOD);
  }
  //测试是否可以正确处理顾客名字不为空的登陆
  public void testLoginPost2() throws ServletException, IOException {
    //测试是否正确设置action参数和name参数
    assertEquals("商品浏览窗口", request.getParameter("action"));
    assertEquals("jack", request.getParameter("name"));
    goodsView.doPost(request, response);
    //正确登陆,购物车对象不为空
    this.assertNotNull(session.getAttribute("cart"));
    Cart cart = (Cart)session.getAttribute("cart");
    this.assertEquals("顾客名字应该是jack", "jack", cart.getCustomer());
  }
  //为doPost方法传入参数的方法
  public void beginAddGoods(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "加入购物车", WebRequest.POST_METHOD);
    theRequest.addParameter("idsLength", "12", WebRequest.POST_METHOD);
    //为购物车增加两个商品
    theRequest.addParameter("goodsId0", "1@@苹果汁@@每箱24瓶@@18.00", WebRequest.POST_METHOD);
    theRequest.addParameter("quantity0", "10", WebRequest.POST_METHOD);
    theRequest.addParameter("goodsId1", "2@@牛奶@@每箱24瓶@@19.00", WebRequest.POST_METHOD);
    theRequest.addParameter("quantity1", "10", WebRequest.POST_METHOD);
  }
  //测试添加商品的方法
  public void testAddGoods() throws ServletException, IOException {
    //为session对象添加购物车对象
    session.setAttribute("cart", new Cart());
    //通过Servlet的doPost方法将商品加入购物车
    goodsView.doPost(request, response);
    Cart cart = (Cart)session.getAttribute("cart");
    this.assertEquals("购物车的商品应该为2件", 2, cart.getGoods().size());
    Object[][] goodsObjs = cart.getGoodsobjs();
    this.assertEquals("总金额应该是370", 370.0, ((Double)goodsObjs[2][5]).doubleValue(),0);
  }
}

⌨️ 快捷键说明

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