题目:
购物车需求
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);
}
}