mercredi 23 janvier 2019

How to test this void methods with JUnit? [duplicate]

This question already has an answer here:

:)

I'm trying to do some JUnit tests on my code. And I don't know really how to start with void methods. What test I can prepare on this method?

I already create tests for "add product" and "get price", "get name" but their not void methods. I use assertEquald(), assertNotNull() etc.

private Map<Product, Integer> basketCase = new HashMap<>();   

public void addProduct(Product product, int quantity) {
    if (quantity < 0) throw new IllegalArgumentException("Quantity cannot be negative!");
    if (!doesProductExist(basketCase, product)) {
        basketCase.put(product, quantity);
    } else {
        int increasedQuantity = basketCase.get(product).intValue()+ quantity;
        basketCase.put(product, increasedQuantity);
    }
}

public boolean doesProductExist(Map<Product, Integer> basket, Product product) {
    if (basket.containsKey(product)) return true;
    else return false;
}

I need any test that will be helpful to learn property JUnit testing :)

Aucun commentaire:

Enregistrer un commentaire