I had this problem today. This is caused by layout calls to set up an interrupt that actually absorbs the hub already in place.
In this example, change the first line to
when(mock.call(anyString())).thenReturn("","",string1,string2)
This will give you two empty answers when you customize your other layout by returning string1 as the first useful return value.
Try also the doReturn option, which I think may not have these problems:
doReturn(string1,string2).when(mock).call(anyString());
During installation, the plug is used in different ways.
So, I did some more research. Here's the function I played with based on the OP question:
Function<String, String> function = mock(Function.class); when(function.apply(anyString())).thenReturn("A","B","C"); when(function.apply("Jim")).thenReturn("Jim"); when(function.apply("Bob")).thenReturn("Bob"); assertThat(function.apply("Jim")).isEqualTo("Jim"); assertThat(function.apply("Bob")).isEqualTo("Bob"); assertThat(function.apply("")).isEqualTo("A"); assertThat(function.apply("")).isEqualTo("B"); assertThat(function.apply("")).isEqualTo("C"); assertThat(function.apply("")).isEqualTo("C");
The above does not work in isEqualTo("A")
because the two calls to configure mocks for Jim
and Bob
consume the return values ββfrom the list provided in anyString()
.
You may be tempted to change the order of the when
clauses, but this fails because anyString()
replaces special cases, so it also fails to execute.
The next version above works as expected:
when(function.apply(anyString())).thenReturn("A","B","C"); doReturn("Jim") .when(function) .apply("Jim"); doReturn("Bob") .when(function) .apply("Bob"); assertThat(function.apply("Jim")).isEqualTo("Jim"); assertThat(function.apply("Bob")).isEqualTo("Bob"); assertThat(function.apply("")).isEqualTo("A"); assertThat(function.apply("")).isEqualTo("B"); assertThat(function.apply("")).isEqualTo("C"); assertThat(function.apply("")).isEqualTo("C");
This is due to the fact that the doReturn
method, designed to modify pre-existing layouts in flight, does not actually include calling the method on the layout in order to set up a mockery.
You can use doReturn
for all settings, and not for mixing between when
... thenReturn
and doReturn
.. when
.. function()
. As it happens, it's a little uglier:
doReturn("A").doReturn("B").doReturn("C") .when(function) .apply(anyString());
There is no convenient varargs
function that allows you to specify multiple returns in a sequence. The above has been verified and really works.