Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the first improvement would be to use this: </p> <pre><code>when(methodA()).thenReturn(linkedListA.get(0), linkedListA.get(1), linkedListA.get(2)) and so on </code></pre> <p>Moreover, you could you the <code>thenAnswer</code> method to return a value:</p> <pre><code>final AtomicInteger i = new AtomicInteger(0); when(methodA()).thenAnswer(new Answer&lt;YourType&gt;() { @Override public YourType answer(InvocationOnMock invocation) { return linkedListA.get(i.getAndIncrement()); } }); </code></pre> <p>Example:</p> <pre><code>import static org.powermock.api.mockito.PowerMockito.when; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(Blub.class) public class Mockexample { @Test public void test() { Blub blub = PowerMockito.mock(Blub.class); final List&lt;Integer&gt; counter = Arrays.asList(1, 2, 3); final AtomicInteger i = new AtomicInteger(0); // blub.size() is final, only reason to use PowerMockito here when(blub.size()).thenAnswer(new Answer&lt;Integer&gt;() { @Override public Integer answer(InvocationOnMock invocation) throws Throwable { return counter.get(i.getAndIncrement()); } }); System.out.println(blub.size()); System.out.println(blub.size()); System.out.println(blub.size()); } } </code></pre> <p>The Blub clas:</p> <pre><code>public class Blub { public final int size() { return 0; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload