Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perhaps the following (using JMockit instead of jMock)?</p> <pre><code> import java.util.*; import org.junit.*; import org.junit.runner.*; import org.hamcrest.*; import static org.hamcrest.core.AnyOf.*; import static org.hamcrest.core.Is.*; import org.hamcrest.core.*; import mockit.*; import mockit.integration.junit4.*; @RunWith(JMockit.class) public class FooTest { public interface Bar { void doIt(String arg); } public class Foo { private Bar bar; public void executeEven(final List&lt;String> allParameters) { for (int i = 0; i &lt; allParameters.size(); i++) { if (i % 2 == 0) { bar.doIt(allParameters.get(i)); } } } public void setBar(final Bar bar) { this.bar = bar; } } @Test public void testExecuteEven(final Bar bar) { final List&lt;String> allParameters = new ArrayList&lt;String>(); final List&lt;Matcher&lt;? extends String>> expectedParameters = new ArrayList&lt;Matcher&lt;? extends String>>(); for (int i = 0; i &lt; 3; i++) { allParameters.add("param" + i); if (i % 2 == 0) { expectedParameters.add(new IsEqual&lt;String>("param" + i)); } } new Expectations() { { bar.doIt(with(anyOf(expectedParameters))); repeats(expectedParameters.size()); } }; Foo subject = new Foo(); subject.setBar(bar); subject.executeEven(allParameters); } @Test // a shorter version of the same test public void testExecuteEven2(final Bar bar) { final List&lt;String> allParameters = Arrays.asList("param0", "param1", "param2"); new Expectations() { { bar.doIt(with(anyOf(is("param0"), is("param2")))); repeats(2); } }; Foo subject = new Foo(); subject.setBar(bar); subject.executeEven(allParameters); } } </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. This table or related slice is empty.
    1. 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