RaghuNaveen
RaghuNaveen

Reputation: 21

Return different values for different input mocking arguments

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

Answers (1)

jayant
jayant

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

Related Questions