Reputation: 21
I encountered a situation where I need to return different values when there is a change in the input, both return value and input parameters are String
.
MethodA.getProperty("", "", "A");
MethodA.getProperty("", "", "B");
The Mockito code I am trying to implement is like
Mockito.when(MethodA.getProperty(Mockito.any(),Mockito.any(),Mockito.anyString())).thenReturn("Apple")
Mockito.when(MethodA.getProperty(Mockito.any(),Mockito.any(),Mockito.anyString())).thenReturn("Banana")
I should get Apple for A, Banana for B
.
please help me on this, thanks in advance.
Upvotes: 0
Views: 1056
Reputation: 406
You can compare the argument while mocking to return specific values. Example:
Mockito.when(MethodA.getProperty(Mockito.any(),Mockito.any(), ArgumentMatchers.argThat(x -> x.equals("A")))).thenReturn("Apple")
Mockito.when(MethodA.getProperty(Mockito.any(),Mockito.any(), ArgumentMatchers.argThat(x -> x.equals("B")))).thenReturn("Banana")
Upvotes: 1