Recently I encountered a piece of code that, in effect, although a lot more verbose, did something along the following lines
All allowed operations for the a
user are fetched and added to it.
Suppose we need a
User with her
allowedOperations in another class
and achieve this by reusing the method above. In the Unit Test of the other
class we would then be stubbing the method
addAllowedOperations to provide us with the
allowedOperations already set.
Stubbing methods which return something, using the Mockito BDD syntax, looks as follows
addAllowedOperations does not return anything and instead
mutates the argument
user, we can’t use the
willReturn structure above.
We need to use the Mockito
It does not look as straightforward as
In fact I had to check the documentation to find out the Mockito syntax
for this apparent sorcery.
Thus by taking into account the added complexity of the test setup a
developer might, in the future, opt for a design which is easier to
test. Even if the method simply returns the mutated
App object instead