Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 283989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:25:13+00:00 2026-05-12T05:25:13+00:00

This is an example of a pattern I’ve encountered a lot recently. I have

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:25:13+00:00Added an answer on May 12, 2026 at 5:25 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this example on StackOverflow: var people = new List<Person> { new
Consider this example (typical in OOP books): I have an Animal class, where each
Following this example, I can list all elements into a pdf file import pyPdf
I had spend some time on this Bridge pattern example from wikipedia , however,
I have a string ( $string for this example) and I want to do
I have code very similar to this example three times in a code behind.
I have a C (not C++) struct that goes like this typedef struct mystruct{
This example is from php.net: <?php function Test() { static $a = 0; echo
Given this example: <img class=a /> <img /> <img class=a /> <img class=a id=active
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.