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

📄 testcart.java~78~

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

import junit.framework.*;
import data.*;
import java.util.*;

public class TestCart extends TestCase {
  private Cart cart = null;

  protected void setUp() throws Exception {
    super.setUp();
    cart = new Cart();
    //设置顾客名字
    cart.setCustomer("jack");
    //创建购物车的测试商品
    Hashtable goods = new Hashtable();
    goods.put("1", "糖果@@每箱30盒@@10@@5");
    goods.put("2", "花生@@每箱30包@@10.0@@20");
    goods.put("3", "花生A@@每箱15包@@10.0@@5");
    cart.addGoods(goods);
  }

  protected void tearDown() throws Exception {
    cart = null;
    super.tearDown();
  }
  //测试购物车的商品删除方法
  public void testRemoveGoods() {
    Collection goodsId = new ArrayList();
    goodsId.add("1");
    goodsId.add("2");
    cart.removeGoods(goodsId);
    this.assertEquals("购物车剩余的商品数", 1, cart.getGoods().size());
  }

  //测试购物车的商品增加方法
  public void testAddGoods() {
    Hashtable goods = new Hashtable();
    //向购物车添加相同的商品
    goods.put("1", "糖果@@每箱30盒@@10@@5");
    cart.addGoods(goods);
    this.assertEquals("购物车剩余的商品数", 3, cart.getGoods().size());
    //向购物车添加不相同的商品
    goods.put("5", "糖果A@@每箱30盒@@10@@5");
    cart.addGoods(goods);
    this.assertEquals("购物车剩余的商品数", 4, cart.getGoods().size());
  }

  //测试购物车的商品更新方法
  public void testUpdateGoods() {
    Hashtable goods = new Hashtable();
    goods.put("1", "糖果@@每箱30盒@@20@@5");
    cart.updateGoods(goods);
    Object[][] actualReturn = cart.getGoodsobjs();
    this.assertEquals("购物车的总金额是350", 350.0,
                      ((Double)actualReturn[4][6]).doubleValue(), 0);
  }

  //测试取得购物车数组的方法
  public void testGetGoodsobjs() {
    Object[][] actualReturn = cart.getGoodsobjs();
    this.assertEquals("购物车的总金额是300", 300.0,
                      ((Double)actualReturn[4][6]).doubleValue(), 0);
  }

  //测试取得商品数组的方法
  public void testGetGoods() {
    Hashtable actualReturn = cart.getGoods();
    assertEquals("return value", 3, actualReturn.size());
  }

  //测试取得顾客名字的方法
  public void testGetCustomer() {
    String expectedReturn = "jack";
    String actualReturn = cart.getCustomer();
    assertEquals("return value", expectedReturn, actualReturn);
  }

  //测试设置顾客名字的方法
  public void testSetCustomer() {
    String customer = "jack1";
    cart.setCustomer(customer);
    assertEquals("return value", customer, cart.getCustomer());
  }

  //测试分离字符串的方法
  public void testDecodeGoodsStr() {
    String goodStr = "糖果@@每箱30盒@@10@@5";
    String[] actualReturn = cart.decodeGoodsStr(goodStr);
    assertEquals("return value", "每箱30盒", actualReturn[1]);
  }
}

⌨️ 快捷键说明

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