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

  • Home
  • SEARCH
  • 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 6735519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:01:18+00:00 2026-05-26T11:01:18+00:00

I’m wondering how to go about checking that a method returns a container encapsulating

  • 0

I’m wondering how to go about checking that a method returns a container encapsulating some collection which is the aggregate of multiple other containers returned by mock objects. That is, it contains all the elements of the individual containers. I have some tests elsewhere that check the container ‘works’ (add/addAll/etc), so I know that works, but I’m not sure how go about with the test below ‘createsRoadUsersAccordingToAllAddedCreators’.

I have a RoadUserCreationDaemon class which I call create upon which returns a RoadUserContainer according to added RoadUserCreator’s. A simplified version:

public class RoadUserCreationDaemon {

    private SimulationManager simulationManager;
    private List<RoadUserCreator> roadUserCreators;

    public RoadUserCreationDaemon(SimulationManager simulationManager) {
        this.simulationManager = simulationManager;
        roadUserCreators = new ArrayList<RoadUserCreator>();
    }

    public void addRoadUserCreator(RoadUserCreator roadUserCreator) {
        roadUserCreators.add(roadUserCreator);
    }

    public RoadUserContainer createRoadUsers() {
        RoadUserContainer roadUsers = new RoadUserContainerImpl(); 
        for (RoadUserCreator creator : roadUserCreators) {
            roadUsers.addAll(createRoadUsers(creator));
        }
        return roadUsers;
    }

    public RoadUserContainer createRoadUsers(
            RoadUserCreator roadUserCreator) {
        return roadUserCreator.create();
    }
}

I started by writing a test (JUnit4 / JMock2.5.1) for createRoadUsers which returns a RoadUserContainer with a supplied creator. Then I started writing a test for a non-parameterised createRoadUsers to see if it returns a container with all the elements of the individual containers returned by the creators:

@RunWith(JMock.class)
public class TestRoadUserCreationDaemon {
    Mockery context = new JUnit4Mockery();      
    private RoadUserCreationDaemon daemon;      
    private RoadUserCreator roadUserCreator;    
    private SimulationManager simulationManager;        
    private RoadUserContainer createdRoadUsers;

    @Before
    public void setUp() {
        simulationManager = context.mock(SimulationManager.class);
        daemon = new RoadUserCreationDaemon(simulationManager);

        roadUserCreator = context.mock(RoadUserCreator.class);
        createdRoadUsers = context.mock(RoadUserContainer.class);
    }       

    @Test
    public void createsRoadUsersAccordingToAllAddedCreators() throws Exception {
        final RoadUserCreator anotherRoadUserCreator = context.mock(RoadUserCreator.class, "anotherRUC");
        final RoadUserContainer moreCreatedRoadUsers = context.mock(RoadUserContainer.class, "moreCRU");
        context.checking(new Expectations() {{
            oneOf (roadUserCreator).create(); will(returnValue(createdRoadUsers));
            oneOf (anotherRoadUserCreator).create(); will(returnValue(moreCreatedRoadUsers));

            oneOf (createdRoadUsers).roadUsersAsList();
            oneOf (moreCreatedRoadUsers).roadUsersAsList();
        }});

        daemon.addRoadUserCreator(roadUserCreator);
        daemon.addRoadUserCreator(anotherRoadUserCreator);
        daemon.createRoadUsers();

        //how to easily check that the two lists are equivilant - have same items, but not the same object?
        //assertEquals(createdRoadUsers, daemon.createRoadUsers() );
    }

    @Test
    public void createsRoadUsersAccordingToCreator() throws Exception {

        context.checking(new Expectations() {{
            oneOf (roadUserCreator).create(); will(returnValue(createdRoadUsers));
        }});
        assertEquals(createdRoadUsers, daemon.createRoadUsers(roadUserCreator));
    }
}

As the comment says…I’m not sure how to proceed in a non-ugly way.

The ‘RoadUserContainer’ interface:

public interface RoadUserContainer extends Iterable<RoadUser> {
    public void add(RoadUser roadUser);
    public Iterator<RoadUser> iterator();
    public void addAll(RoadUserContainer createRoadUsers);
    public List<RoadUser> roadUsersAsList();
    public boolean equals(RoadUserContainer otherContainer);
    ...
}

I am new to TDD and mocking, and this is my first Java project for >6 years, so feel free to comment on ancillary aesthetics!

  • 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-26T11:01:19+00:00Added an answer on May 26, 2026 at 11:01 am

    I would probably initially use real containers and mock the other objects. Then use hamcrest to interrogate the resulting object.

    The test I would want to create would look something like this:

    final RoadUser roadUser0 = context.mock(RoadUser.class, "roadUser0");
    final RoadUser roadUser1 = context.mock(RoadUser.class, "roadUser1");
    final RoadUser roadUser2 = context.mock(RoadUser.class, "roadUser2");
    
    final RoadUserCreator roadUserCreator0 = context.mock(RoadUserCreator.class, "roadUserCreator0");
    final RoadUserCreator roadUserCreator1 = context.mock(RoadUserCreator.class, "roadUserCreator1");
    
    final RoadUserCreationDaemon daemon = new RoadUserCreationDaemon(null);
    daemon.addRoadUserCreator(roadUserCreator0);
    daemon.addRoadUserCreator(roadUserCreator1);        
    
    context.checking(new Expectations() {{
        oneOf(roadUserCreator0).create(); will(returnValue(roadUsers(roadUser0, roadUser1)));
        oneOf(roadUserCreator1).create(); will(returnValue(roadUsers(roadUser2)));
    }});
    
    assertThat(daemon.createRoadUsers(), contains(roadUser0, roadUser1, roadUser2));
    

    you will need these imports from hamcrest:

    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.Matchers.contains;
    

    If order is not important you could use containsInAnyOrder instead of contains

    you would also need to create the utility method “roadUsers”

    public static RoadUserContainer roadUsers(final RoadUser... roadUsers)
    {
        return new RoadUserContainerImpl(roadUsers);
    }
    

    An alternative design would be to change the interface of the RoadUserCreationDaemon

    public void createRoadUsers(final RoadUserContainer roadUsers) {
        for (final RoadUserCreator roadUserCreator : roadUserCreators) {
            roadUsers.addAll(roadUserCreator.create());
        }
    }
    

    Then you could write the tests like this:

    final RoadUserContainer roadUserContainer0 = context.mock(RoadUserContainer.class, "roadUserContainer0");
    final RoadUserContainer roadUserContainer1 = context.mock(RoadUserContainer.class, "roadUserContainer1");
    
    final RoadUserContainer resultRoadUserContainer = context.mock(RoadUserContainer.class, "resultRoadUserContainer");
    
    final RoadUserCreator roadUserCreator0 = context.mock(RoadUserCreator.class, "roadUserCreator0");
    final RoadUserCreator roadUserCreator1 = context.mock(RoadUserCreator.class, "roadUserCreator1");
    
    final RoadUserCreationDaemon daemon = new RoadUserCreationDaemon(null);
    daemon.addRoadUserCreator(roadUserCreator0);
    daemon.addRoadUserCreator(roadUserCreator1);
    
    context.checking(new Expectations() {
        {
            oneOf(roadUserCreator0).create();
            will(returnValue(roadUserContainer0));
            oneOf(roadUserCreator1).create();
            will(returnValue(roadUserContainer1));
    
            oneOf(resultRoadUserContainer).addAll(roadUserContainer0);
            oneOf(resultRoadUserContainer).addAll(roadUserContainer1);
        }
    });
    
    daemon.createRoadUsers(resultRoadUserContainer);
    

    If the order of the calls to “addAll” is important you can use a jmock sequence

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from

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.