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?
getShoppingCart()method has side-effects, you don't need to test that it's called. If it does have side effects, you should really change its name becausegetXXX()methods conventionally should be idempotent. – Jules Nov 26 '17 at 02:41getNextValue? Arguably, someone could say "Don't call it a getter; change the name tonextValue", but I have seengetNextused before. Perhaps a better example would be an object representing an electron; what happens when I callgetPosition? Or worse,getPosition(); getVelocity();– Aaron Nov 30 '17 at 22:14