I am new to Mockito, I have tried looking into this Exception but I haven´t found a concrete answer. It happens in my code when I use two mocks together, meaning that I give
There are some good fixes posted in this question already, but for anyone still having trouble understanding it, think of the order in which Java calls all those methods. According to the Java Language Specification, Java evaluates every parameter of a method left-to-right before calling the method:
integerValue.getValue(), which Mockito recordswhen, where Mockito takes the last call (to integer.getValue) and starts setting up stubbingvalue.toString, which is a mocked call that Mockito recordsthenReturn on the stubberMockito complains exactly because the call to the mock, step 3, happens after step 2 (when) but before step 4 (thenReturn), causing the validation framework to complain about the stubbing. Joy, your answer moves the troublesome step 3 to before step 1, which is fine; Sajan removes it from the statement entirely, which is also fine.