title: Mocking objects that encapsulate collections
body: <p>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'. </p>
  
  <p>I have a RoadUserCreationDaemon class which I call create upon which returns a RoadUserContainer according to added RoadUserCreator's. A simplified version:</p>
  
  <pre><code>public class RoadUserCreationDaemon {
  
      private SimulationManager simulationManager;
      private List&lt;RoadUserCreator&gt; roadUserCreators;
  
      public RoadUserCreationDaemon(SimulationManager simulationManager) {
          this.simulationManager = simulationManager;
          roadUserCreators = new ArrayList&lt;RoadUserCreator&gt;();
      }
  
      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();
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>@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));
      }
  }
  </code></pre>
  
  <p>As the comment says...I'm not sure how to proceed in a non-ugly way.</p>
  
  <p>The 'RoadUserContainer' interface:</p>
  
  <pre><code>public interface RoadUserContainer extends Iterable&lt;RoadUser&gt; {
      public void add(RoadUser roadUser);
      public Iterator&lt;RoadUser&gt; iterator();
      public void addAll(RoadUserContainer createRoadUsers);
      public List&lt;RoadUser&gt; roadUsersAsList();
      public boolean equals(RoadUserContainer otherContainer);
      ...
  }
  </code></pre>
  
  <p>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! </p>