Java编写测试用例

该博客主要涉及Java编程,讲述了如何创建ShoppingCart类、两个异常类以及Item类,并详细说明了如何编写针对这些类的测试用例,确保代码的正确性和功能完备性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

购物车需求

ShoppingCart类的主要功能是能增加和删除商品。增加的商品可以是null。
针对它的addItems和deleteItems编写单元测试。API说明如下:
/**
 * 增加商品到购物车
 * @param item
 * @param count
 * @throws NegativeCountException count为负数时,抛NegativeCountException
 */
public void addItems(Item item, int count) throws NegativeCountException

/**
 * 从购物车删除商品
 * @param item
 * @param count
 * @throws NegativeCountException count为负数时,抛NegativeCountException
 * @throws NoSuchItemException 删除的商品不存在,抛NoSuchItemException
*/
public void deleteItems(Item item, int count) throws NegativeCountException,NoSuchItemException

1.创建ShoppingCart类

package com.zte.practice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCar {


    public Map<Item, Integer> map = new HashMap<>();

    /**
     * 增加商品到购物车
     *
     * @param item
     * @param count
     * @throws NegativeCountException count为负数时,抛NegativeCountException
     *                                ShoppingCart类的主要功能是能增加和删除商品。增加的商品可以是null。
     *                                针对它的addItems和deleteItems编写单元测试。API说明如下:
     */
    public void addItems(Item item, int count) throws NegativeCountException {
        if (count < 0)
            throw new NegativeCountException("count cannot be negative");
        if (map.get(item) != null) {
            map.put(item, map.get(item) + count);
        } else {
            map.put(item, count);
        }

    }

    /**
     * 从购物车删除商品
     *
     * @param item
     * @param count
     * @throws NegativeCountException count为负数时,抛NegativeCountException
     * @throws NoSuchItemException    删除的商品不存在,抛NoSuchItemException
     */
    public void deleteItems(Item item, int count) throws NegativeCountException, NoSuchItemException {
        if (map.get(item) != null) {
            if (count >= 0) {
                if (map.get(item) >= count) {
                    int num = map.get(item) - count;
                    map.put(item, num);

                } else {
                    throw new NegativeCountException("there is not enough!");
                }

            } else if (count < 0) {
                throw new NegativeCountException("count cannot be negative ");
            }
        } else {
            throw new NoSuchItemException("no such items!");
        }
    }
}

2.创建异常类

NoSuchItemException
package com.zte.practice;

public class NoSuchItemException extends Exception {
    public NoSuchItemException(String message) {
        super(message);
    }
}

 

3.创建异常类

NegativeCountException
package com.zte.practice;

public class NegativeCountException extends Exception {
    public NegativeCountException(String message) {
        super(message);
    }
}

4.创建Item类

package com.zte.practice;

import java.util.Objects;

public class Item {

    private String name;

    public Item() {
    }

    public Item(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
   //重写equal方法
   public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Item item = (Item) o;
        return name.equals(item.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

 

5.写测试用例

package com.zte.practice;

import com.zte.Dbservice;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ShoppingCarTest {
    @Mock
    private Dbservice dbservice = mock(Dbservice.class);
    private ShoppingCar shoppingCar;
    public Map<Item, Integer> map = new HashMap<>();
    @Before
    public void setUp() {
        shoppingCar = new ShoppingCar();
        List<Item> list1 = new ArrayList<>();
        Item milk = new Item();
        milk.setName("milk");
        List<Item> list2 = new ArrayList<>();
        Item cheese = new Item();
        cheese.setName("cheese");
        shoppingCar.map.put(milk, 9);
        shoppingCar.map.put(cheese, 8);
    }


    @Test
    public void when_not_exsit_should_add_one() throws NegativeCountException {
        Item item = new Item("apple");
        shoppingCar.addItems(item, 9);
        int num = shoppingCar.map.get(item);
        Assert.assertEquals(9, num);

    }

    @Test
    public void when_exsit_should_plus_before_count() throws NegativeCountException {
        Item item = new Item("milk");
        shoppingCar.addItems(item, 1);
        int num = shoppingCar.map.get(item);
        Assert.assertEquals(10, num);
    }


    @Test(expected = NegativeCountException.class)
    public void should_add_one_NegativeCountException() throws NegativeCountException {
        Item item = new Item("milk");
        shoppingCar.addItems(item, -1);
        int num = shoppingCar.map.get("milk");
        Assert.assertEquals(9, num);
    }

    @Test
    public void should_add_null() throws NegativeCountException {
        Item item;
        shoppingCar.addItems(null, 9);
        int num = shoppingCar.map.get(null);
        Assert.assertEquals(9, num);
    }


    @Test
    public void should_delete_have_exist_one() throws NegativeCountException, NoSuchItemException {
        Item item = new Item("milk");
        shoppingCar.deleteItems(item, 1);
        int num = shoppingCar.map.get(item);
        Assert.assertEquals(8, num);
    }

    @Test(expected = NegativeCountException.class)
    public void should_delete_ten_NegativeCountException() throws NegativeCountException, NoSuchItemException {
        Item item = new Item("milk");
        shoppingCar.deleteItems(item, 10);
    }

    @Test(expected = NegativeCountException.class)
    public void when_delete_count_negative_NegativeCountException() throws NegativeCountException, NoSuchItemException {
        Item item = new Item("milk");
        shoppingCar.deleteItems(item, -1);
    }

    @Test(expected = NoSuchItemException.class)
    public void should_delete_one_NoSuchItemException() throws NegativeCountException, NoSuchItemException {
        Item item = new Item("bnana");
        shoppingCar.deleteItems(item, 10);
    }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值