Calling Methods of Another Class during Unit Testing

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

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 use the unit test method calculateTotalwith junit and mockito, but for this I need to add some mocked instances of MyObject to m_listOfObjects. However, this would mean a call to another method in the test calculatedTotal, addObject.

Is this a valid unit test, or is it against best practices since my test calculateTotalalso depends on the method addObject?

+4
source share
1

"" . , Mocks.

, , , . , .

- unit test - , , .

+6

All Articles