English 中文(简体)
2. 收集的 objects
原标题:Mocking objects that encapsulate collections

我不禁要问,如何检查一种方法回收一个集装箱,把一些收集归起来,这些集装箱是用模拟物体返回的多个其他集装箱的集合。 也就是说,它包含每个集装箱的所有内容。 我在其他地方进行了一些检查,检查了集装箱工程(add/addAll/etc),因此我知道这些工程,但我不敢肯定,下面的测试如何进行。 无国界医生组织。

我有一个路UserCreationDaemon级,我呼吁建立这样的班子,根据“路UserCreator”号补充材料,将一名路UserContainer送回。 简化版本:

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();
    }
}

我首先撰写了一份试验(JUnit4 / JMock 2.5.1),用于制造路标器,用一个供应的制造器把路UserContainer送回。 然后,我开始撰写非参数制造试验。 公路用户看看看它是否回收了集装箱,加上各集装箱的所有成分,由制造者返还:

@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));
    }
}

评论说...... 我不敢肯定如何以非建议的方式行事。

公路用户界面:

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

我是TDD和模拟的新鲜事,这是我的第一个 Java项目,为期6年,因此可以自由评论辅助美学!

最佳回答

我可能首先使用真正的集装箱和ck其他物体。 然后使用hamcrest,对由此产生的物体进行拦截。

我想要创造的检验标准就是这样:

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

您将需要这些从头盔进口的物品:

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

如果命令不重要,你可以使用 InAnyOrder,

您还需要制定“道路用户”的实用方法。

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

另一种设计是改变道路UserCreationDaemon的界面。

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

然后,你可以写照:

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

如果要求“满足所有人”的命令很重要,你可以使用jmock序列

问题回答

我认为,我会变造者,但会回去真正的集装箱。 检验的想法是,确保Daemon援引所有创作者制造方法,正确吗? 因此,你们的测试条件就好像是这样。

  RoadUserContainer result = daemon.createRoadUsers();

  // Check that the result contains both creator s users
  Assert.assertEquals(createdRoadUsers.size() + moreCreatedRoadUsers.size(), result.size());

  for (RoadUser user : createdRoadUsers)
      Assert.assertTrue(result.contains(user));
  for (RoadUser user : moreCreatedRoadUsers)
      Assert.assertTrue(result.contains(user));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签