Mockito UnfinishedStubbingException

前端 未结 4 1314
离开以前
离开以前 2020-12-09 08:49

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

4条回答
  •  半阙折子戏
    2020-12-09 09:31

    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:

    1. integerValue.getValue(), which Mockito records
    2. when, where Mockito takes the last call (to integer.getValue) and starts setting up stubbing
    3. value.toString, which is a mocked call that Mockito records
    4. thenReturn on the stubber

    Mockito 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.

提交回复
热议问题