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

📄 testcartview.java~71~

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

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

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);
    request.setCharacterEncoding("GBK");
  }

  protected void tearDown() throws Exception {
    cartView = null;
    super.tearDown();
  }
  //为doPost方法传入参数的方法
  public void beginUpdateGoods(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "修改购物车的数量", WebRequest.POST_METHOD);
    theRequest.addParameter("idsLength", "2", WebRequest.POST_METHOD);
    theRequest.addParameter("goodsId1", "2@@牛奶@@每箱24瓶@@19.00",
                            WebRequest.POST_METHOD);
    theRequest.addParameter("quantity1", "5", WebRequest.POST_METHOD);
  }
  //测试更新购物车商品数量的方法
  public void testUpdateGoods() throws ServletException, IOException {
    //创建测试的购物车
    Cart cart = new Cart();
    //创建购物车的测试商品
    Hashtable goods = new Hashtable();
    goods.put("1", "苹果汁@@每箱24瓶@@18.00@@10");
    goods.put("2", "牛奶@@每箱24瓶@@19.00@@10");
    cart.addGoods(goods);
    //为session对象添加购物车对象
    session.setAttribute("cart", cart);
    //通过Servlet的doPost方法更改商品的数量
    cartView.doPost(request, response);
    //从session对象取得购物车对象
    cart = (Cart)session.getAttribute("cart");
    Object[][] goodsObjs = cart.getGoodsobjs();
    this.assertEquals("总金额应该是275", 275.0, ((Double)goodsObjs[2][5]).doubleValue(),0);
  }
  //为doPost方法传入参数的方法
  public void beginRemoveGoods(WebRequest theRequest) {
    //通过WebRequest类设置传入参数
    theRequest.addParameter("action", "删除选择商品", WebRequest.POST_METHOD);
    theRequest.addParameter("idsLength", "2", WebRequest.POST_METHOD);
    theRequest.addParameter("goodsId1", "2@@牛奶@@每箱24瓶@@19.00",
                            WebRequest.POST_METHOD);
    theRequest.addParameter("quantity1", "5", WebRequest.POST_METHOD);
  }
  //测试删除购物车商品数量的方法
  public void testRemoveGoods() throws ServletException, IOException {
    //创建测试的购物车
    Cart cart = new Cart();
    //创建购物车的测试商品
    Hashtable goods = new Hashtable();
    goods.put("1", "苹果汁@@每箱24瓶@@18.00@@10");
    goods.put("2", "牛奶@@每箱24瓶@@19.00@@10");
    cart.addGoods(goods);
    //为session对象添加购物车对象
    session.setAttribute("cart", cart);
    //通过Servlet的doPost方法删除商品
    cartView.doPost(request, response);
    //从session对象取得购物车对象
    cart = (Cart)session.getAttribute("cart");
    this.assertEquals("购物车的商品应该为1件", 1, cart.getGoods().size());
    Object[][] goodsObjs = cart.getGoodsobjs();
    this.assertEquals("总金额应该是180", 180.0, ((Double)goodsObjs[1][5]).doubleValue(),0);
  }
}

⌨️ 快捷键说明

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