如何使用JMock在模拟方法中测试模拟方法 [英] How to use JMock to test mocked methods inside a mocked method

查看:297
本文介绍了如何使用JMock在模拟方法中测试模拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在确定如何模拟特定代码方面遇到麻烦.

I'm having trouble determining how to mock a particular piece of code.

这是我的方法:

public void sendNotifications(NotificationType type, Person person)
    {
        List<Notification> notifications = findNotifications(type);
        for(Notification notification : notifications)
        {
            notification.send(person);
        }
    }

我希望能够使用JMock来测试是否调用了findNotifications并返回期望值并调用了send().

I would like to be able to use JMock to test that findNotifications is called and that and returns the expected value and that send() is called.

findNotifications称我的Dao被嘲笑了.通知是一个抽象类.

findNotifications calls my Dao which is mocked. Notification is an abstract class.

我有这样的单元测试,但是显然不起作用.它满足了前两个期望,但仅此而已.

I have a unit test like this, but it's obviously not working. It satisfies the first two expectations but no more.

    @Test
public void testSendNotifications2()
{
    final Person person = new Person();
    notificationEntries.add(createEntry1);

    Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
    final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
    final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");
    notifications.add(mockedNotification);

    final NotifierServiceImpl mockedService = context.mock(NotifierServiceImpl.class, "mockedService");

    //NotifierService service = new NotifierServiceImpl(dao);

    context.checking(new Expectations() {
        {
            one(mockedService).setDao(dao);
            one(mockedService).sendNotifications(NotificationType.CREATE, person);
            one(mockedService).findNotifications(NotificationType.CREATE);
            one(dao).getByNotificationType(NotificationType.CREATE);
            will(returnValue(notificationEntries)); 
            will(returnValue(notifications));

            one(mockedNotification).send(person);
        }
    });

    mockedService.setDao(dao);
    mockedService.sendNotifications(NotificationType.CREATE, person);
    context.assertIsSatisfied();
}

    mockedService.sendNotifications(NotificationType.CREATE, person);
    context.assertIsSatisfied();
}

如何使它按我的意愿工作?

How could I get this to work as I want it to?

我尝试过的另一种方法.它满足前两个期望,但不能满足发送的期望.

Another way I tried. It satisfies the first two expectations but not the send one.

@Test
public void testSendNotifications()
{
    final Person person = new Person();
    notificationEntries.add(createEntry1);

    Mockery context = new JUnit4Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }};
    final NotificationDao dao = context.mock(NotificationDao.class, "notificationDao");
    final Notification mockedNotification = context.mock(V2ADTNotification.class, "mockNotification");

    NotifierService service = new NotifierServiceImpl(dao);

    context.checking(new Expectations() {
        {
            one(dao).getByNotificationType(NotificationType.CREATE);
            will(returnValue(notificationEntries)); 

            one(mockedNotification).send(person);
        }
    });

    service.sendNotifications(NotificationType.CREATE, person);
    context.assertIsSatisfied();
}

我对使用Jmock非常陌生,因此,对于我在做什么(我没有)不太了解,我深表歉意.

I'm pretty new to using Jmock so I apologize if it doesn't look like I have much of a clue as to what I'm doing (I don't).

推荐答案

在模拟方法中测试模拟方法.

test mocked methods inside a mocked method.

您真的做不到.不会进行real方法中的调用,因为您是在调用模拟而不是real方法.

You really just can't do that. The calls in the real method aren't being made, because you're calling the mock instead of the real method.

您的第一次尝试未通过最后两个期望,因为使它们通过的调用仅在sendNotifications的实际实现中进行,而不是在您对它进行的模拟中进行.

Your first attempt fails the last two expectations because the calls that would make them pass are made only in the real implementation of sendNotifications, and not in the mock that you made of it.

第二个更接近于可行,但您需要将mockedNotification添加到已设置为通过getByNotificationType的模拟返回的notificationEntries列表中.

The second is closer to workable, but you need to add your mockedNotification to the notificationEntries list that you've set up to be return by the mock of getByNotificationType.

这篇关于如何使用JMock在模拟方法中测试模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆