I have a quite complicated method for which I want to test the behavior (using Mockito and JUnit). This method takes an object (let's call its type State
) as input, and should take a few different state variables into account for deciding its output.
As an example, considering the following specification (s
is a mock of the State
class):
- If
s.varOne
is set, return its value. - Else, if
s.varTwo
is set, return that instead. - Else, if
s.varThree
is set, calls.update(s.varThree)
and then returns.varOne
, which will now have a value (even though it didn't at stage 1.) - Else, throw an error.
In order to test case 3 properly, I would like to set up the s
object so that s.varOne
and s.varTwo
are both unset to begin with, but if (and only if!) the sut calls s.update(s.varThree)
, then after that s.varOne
returns something.
Is there a good way to setup this behavior in Mockito?
I have considered setting up some chain of return values for s.varOne
, and then verifying that the order of the calls corresponds to the order of the outputs (as well as that the return value of the sut is correct, of course), but this feels dirty; if I then change the method to calculate its return value in some other way, which calls s.varOne
a different number of times but doesn't change its output, then the test will fail even though the functionality is the same.
My ideal solution is a way where I can add some "delayed" setup for the mock object, which is run when the sut calls the s.update()
method, but I can't figure out a way to accomplish that.