I have a method that I call that connects to another server and each time I call it, and it returns different data.
I am writing a unit test for the class calling the method. I have mocked that class and I expect it to return the stubbed result. It actually works using doReturn
, but it returns the same data every time. I want it to return different data and I want to be able to specify what it should be.
I tried using 'doReturn - when' and it works, but I can't make it return a different result. I don't know how to do this.
I also tried using 'when - thenReturn' which was the solution I found here on StackOverflow. With that, I could specify o get different responses every time I called the same method.
Problem is that I get a compilation error
The method XXX is undefined for the type
OngoingStubbing<MyClass>
JSONArray jsonArray1 = { json array1 here };
JSONArray jsonArray2 = { json array2 here };
// Works but return the same jsonArray1 every time:
MyClass MyClassMock = mock(MyClass.class);
Mockito.doReturn(jsonArray1)
.when(MyClassMock).getMyValues(any(List.class), any (String.class), any(String.class),
any(String.class),
any(String.class));
// Does not work:
when(MyClassMock).getMyValues(any(List.class),
any(String.class), any(String.class),
any(String.class),
any(String.class)).thenReturn(jsonArray1, jsonArray2);
// Compile error:
// The method getMyValues(any(List.class), any(String.class), any (String.class), any(String.class), any(String.class)) is undefined for the type OngoingStubbing<MyClass>
I get compile error:
The method getMyValues(any(List.class), any(String.class), any(String.class), any(String.class), any(String.class)) is undefined for the type OngoingStubbing