jeudi 28 mai 2015

How to test adding an element to an arraylist with junit


I want to test addElement method:

public class MyClass {
    private List<Integer> ints = new ArrayList<>();

    public void addElement(int element) {
        ints.add(element);
    }
}

I need to test if my element was added. According to class isolation I can't use any of ArrayList methods to check it. In fact I should mock my arrayList. But if I mock it and change behavior of get() method to return some value I can't be sure if my element is in that list.

How to test it?

Aucun commentaire:

Enregistrer un commentaire