In order to fix a bug in an application, I modified a method named postLogin
by adding a call to an existing method named getShoppingCart
.
Code
protected void postLogin() {
getShoppingCart();
}
However, I'm not sure what the best way to write a unit test for postLogin
is.
Approach 1
Use verify from Mockito to simply verify that the method was called.
verify(mock).getShoppingCart();
Approach 2
Test the side effect of the method call by fetching the value of the user's shopping cart.
AssertNotNull(user.getShoppingCart());
Is one approach better than the other?
Best Answer
I would usually prefer method 2.
Why? Because, you want
postLogin
to change some state of your system, but how it accomplishes this (and which methods it calls internally for this) is merely an implementation detail, nothing your unit test should make any assumptions about. So better make your test just verifying the final state.