Search code examples
javaunit-testingjmockjmockit

Is there a neater way of testing calls to mocked methods for each item in a list


This is an example of a pattern I've encountered a lot recently. I have a method to be tested that takes a List and may invoke some other method(s) for each item in the list. To test this I define an Iterator with the expected call parameters and a loop in the JMock expectations to check the call is made against each item of the iterator (see trivial example below).

I've had a look at the Hamcrest matchers but haven't found something that tests for this (or have misunderstood how the available matchers work). Does anyone have a more elegant approach?

package com.hsbc.maven.versionupdater;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.Sequence;
import org.jmock.internal.NamedSequence;

public class FooTest extends AbstractMojoTestCase {

    public interface Bar {
        void doIt(String arg);
    }

    public class Foo {

        private Bar bar;

        public void executeEven(final List<String> allParameters) {
            for (int i = 0; i < allParameters.size(); i++) {
                if (i % 2 == 0) {
                    bar.doIt(allParameters.get(i));
                }
            }
        }

        public Bar getBar() {
            return bar;
        }

        public void setBar(final Bar bar) {
            this.bar = bar;
        }

    }

    public void testExecuteEven() {
        Mockery mockery = new Mockery();

        final Bar bar = mockery.mock(Bar.class);
        final Sequence sequence = new NamedSequence("sequence");

        final List<String> allParameters = new ArrayList<String>();
        final List<String> expectedParameters = new ArrayList<String>();

        for (int i = 0; i < 3; i++) {
            allParameters.add("param" + i);
            if (i % 2 == 0) {
            expectedParameters.add("param" + i);
            }
        }

        final Iterator<String> iter = expectedParameters.iterator();

        mockery.checking(new Expectations() {
            {
                while (iter.hasNext()) {
                    one(bar).doIt(iter.next());
                    inSequence(sequence);
                }
            }
        });

        Foo subject = new Foo();
        subject.setBar(bar);
        subject.executeEven(allParameters);
        mockery.assertIsSatisfied();
    }
}

Solution

  • I think your current test implementation is pretty close to ideal. Any further compaction risks either changing the semantics of the test or obscuring the intent of the test to a reader (or both).

    However, if you're looking for a way to expect a specific number of calls to a method, you can use exactly(n).of():

    mockery.checking(new Expectations() {{
      exactly(expectedParameters.length()).of(bar).doIt(with(anyOf(expectedParameters)));
    }});
    

    (I left out the evenness check, but you get the idea). This is similar to the jmockit example in a different answer. Be aware that this does not test the same thing as your original test. In particular it does not check:

    1. The order of the calls to doIt
    2. That each element of the parameter list is passed exactly once

    For example, this test would pass if your method iterated over the list in reverse order, or if it just called the doIt method n times but passed the first element of the list each time. If you want to ensure that each element in the list is passed, you pretty much have to iterate over it setting an individual expectation for each. If you don't care about the order of the invocations, you can omit the use of the Sequence (in that case you may want to change your original method to accept a Collection instead of a List).