dimanche 30 août 2015

Calling other class methods in unit testing

I have a class which contains a list of objects in it, which then uses to return the user a calculated value using these objects' states. E.g:

class MyContaier {
    private List<MyObject> m_listOfObjects;

    public MyContainer() {
        ...
    }

    public void addObject(MyObject object) { 
        m_listOfObjects.add(object);
    }

    public int calculateTotal() {
        int total = 0;

        for (MyObject object : m_listOfObjects)
            total += object.getValue();

        return total;
    }
}

I am trying to unit test calculateTotal method using junit and mockito, but in order to do that I need to add a few mocked MyObject instances to m_listOfObjects. However, this would mean calling another method in the calculatedTotal test, addObject.

Is this a valid unit test, or is it against the best practices as my test of calculateTotal also depends on addObject method?

Aucun commentaire:

Enregistrer un commentaire