Problem Descriptoin
Implement unit tests for a shopping cart method using path and loop coverage techniques. The shopping cart functionality handles adding products to a cart collection with the following behaviors:
- When cart is null, create new collection and add product
- When cart is empty, add product directly
- When product exists in cart, increment quantity
- When product doesn't exist, add new product
Requirements
- Use basic path coverage + simple loop coverage methods
- Implement tests in Java with JUnit framework
- Achieve 100% path and loop coverage
- Use provided test data
- Follow code quality standards
Implementation Classes
Product Class
public class Product {
private int id;
private String name;
private String category;
private double price;
private int quantity;
public Product(int id, String name, String category, double price, int quantity) {
this.id = id;
this.name = name;
this.category = category;
this.price = price;
this.quantity = quantity;
}
// Getters and setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
// ... other getters/setters ...
}
CartService Class
public class CartService {
public List<Product> addToCart(List<Product> cart, Product item) {
if (cart == null) {
cart = new ArrayList<>();
cart.add(item);
} else {
boolean exists = false;
for (Product current : cart) {
if (current.getId() == item.getId()) {
current.setQuantity(current.getQuantity() + item.getQuantity());
exists = true;
break;
}
}
if (!exists) {
cart.add(item);
}
}
return cart;
}
}
Test Data Provider
public class TestData {
public static List<Product> nullCart = null;
public static List<Product> emptyCart = new ArrayList<>();
public static List<Product> populatedCart = new ArrayList<>();
public static Product phone1 = new Product(1, "Phone", "BrandA", 3000, 2);
// Additional product definitions
// ...
static {
populatedCart.add(phone1);
// Add more products to populatedCart
}
}
Test Implementation
public class CartServiceTest {
private CartService cartService;
@Before
public void setup() {
cartService = new CartService();
}
@Test
public void testNullCart() {
List<Product> result = cartService.addToCart(TestData.nullCart, TestData.phone1);
assertEquals(1, result.size());
assertEquals(TestData.phone1, result.get(0));
}
@Test
public void testEmptyCart() {
List<Product> result = cartService.addToCart(TestData.emptyCart, TestData.phone1);
assertEquals(1, result.size());
assertEquals(TestData.phone1, result.get(0));
}
@Test
public void testNewProductAddition() {
Product newProduct = new Product(10, "Tablet", "BrandB", 4000, 1);
List<Product> result = cartService.addToCart(TestData.populatedCart, newProduct);
assertTrue(result.contains(newProduct));
}
@Test
public void testExistingProductUpdate() {
Product existing = TestData.phone1;
List<Product> result = cartService.addToCart(TestData.populatedCart, existing);
assertEquals(4, result.get(0).getQuantity());
}
// Additional tests for loop coverage
@Test
public void testMultipleExistingProducts() {
// Tests for different positions in the list
cartService.addToCart(TestData.populatedCart, TestData.phone2);
cartService.addToCart(TestData.populatedCart, TestData.phone3);
// Verify quantity updates
}
}