My Code is as below,
@RunWith(MockitoJUnitRunner.class)
public class MyClass {
private static final String code =\"Test\";
@Mock
private MyCl
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.