How to resolve Unneccessary Stubbing exception

前端 未结 13 1416
无人共我
无人共我 2020-12-24 03:57

My Code is as below,

@RunWith(MockitoJUnitRunner.class)
public class MyClass {

    private static final String code =\"Test\";

    @Mock
     private MyCl         


        
13条回答
  •  [愿得一人]
    2020-12-24 04:53

    Well, In my case Mockito error was telling me to call the actual method after the when or whenever stub. Since we were not invoking the conditions that we just mocked, Mockito was reporting that as unnecessary stubs or code.

    Here is what it was like when the error was coming :

    @Test
    fun `should return error when item list is empty for getStockAvailability`() {
        doAnswer(
            Answer { invocation ->
                val callback =
                    invocation.arguments[1] as GetStockApiCallback
                callback.onApiCallError(stockResultViewStateError)
                null
            }
        ).whenever(stockViewModelTest)
            .getStockAvailability(listOf(), getStocksApiCallBack)
    }
    

    then I just called the actual method mentioned in when statement to mock the method.

    changes done is as below stockViewModelTest.getStockAvailability(listOf(), getStocksApiCallBack)

    @Test
    fun `should return error when item list is empty for getStockAvailability`() {
        doAnswer(
            Answer { invocation ->
                val callback =
                    invocation.arguments[1] as GetStockApiCallback
                callback.onApiCallError(stockResultViewStateError)
                null
            }
        ).whenever(stockViewModelTest)
            .getStockAvailability(listOf(), getStocksApiCallBack)
        //called the actual method here
        stockViewModelTest.getStockAvailability(listOf(), getStocksApiCallBack)
    }
    

    it's working now.

提交回复
热议问题