Skip to content

Exercice : Tests unitaires JUnit

Objectif : pratiquer de nombreux types de tests (valeurs normales, cas limites, null, throw, total, listes…).


Code à tester

Product.java

package com.example.shop;

import java.util.Objects;

public class Product {
    private final String id;
    private final String name;
    private final double price;

    public Product(String id, String name, double price) {
        if (id == null || id.isBlank())
            throw new IllegalArgumentException("id ne doit pas être vide");
        if (name == null || name.isBlank())
            throw new IllegalArgumentException("name ne doit pas être vide");
        if (price < 0)
            throw new IllegalArgumentException("price doit être >= 0");

        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String id() { return id; }
    public String name() { return name; }
    public double price() { return price; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Product other)) return false;
        return Objects.equals(id, other.id);
    }

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

CartItem.java

package com.example.shop;

public class CartItem {
    private final Product product;
    private int quantity;

    public CartItem(Product product, int quantity) {
        if (product == null)
            throw new NullPointerException("product ne doit pas être null");
        if (quantity <= 0)
            throw new IllegalArgumentException("quantity doit être > 0");

        this.product = product;
        this.quantity = quantity;
    }

    public Product getProduct() { return product; }
    public int getQuantity() { return quantity; }

    public void increaseQuantity(int delta) {
        if (delta <= 0)
            throw new IllegalArgumentException("delta doit être > 0");
        this.quantity += delta;
    }

    public double totalPrice() {
        return product.price() * quantity;
    }
}

Cart.java

package com.example.shop;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Cart {
    private final List<CartItem> items = new ArrayList<>();

    public void addItem(CartItem item) {
        if (item == null)
            throw new NullPointerException("item ne doit pas être null");
        items.add(item);
    }

    public List<CartItem> getItems() {
        return Collections.unmodifiableList(items);
    }

    public boolean isEmpty() {
        return items.isEmpty();
    }

    public double total() {
        return items.stream().mapToDouble(CartItem::totalPrice).sum();
    }

    public void clear() {
        items.clear();
    }
}

CartService.java

package com.example.shop;

public class CartService {

    public double applyDiscount(double total, int discountPercent) {
        if (total < 0)
            throw new IllegalArgumentException("total doit être >= 0");
        if (discountPercent < 0 || discountPercent > 100)
            throw new IllegalArgumentException("discountPercent doit être entre 0 et 100");

        double factor = 1 - (discountPercent / 100.0);
        return total * factor;
    }

    public boolean isBigCart(Cart cart) {
        if (cart == null)
            throw new NullPointerException("cart ne doit pas être null");
        return cart.total() >= 100;
    }
}

Travail demandé

Créer 3 classes de tests JUnit :

  • ProductTest
  • CartItemTest
  • CartAndCartServiceTest

Tester :
✔ valeurs normales
✔ cas limites
✔ null
✔ exceptions
✔ calculs
✔ listes
✔ assertAll
✔ assertThrows