簡體   English   中英

如何使用JMock在模擬方法中測試模擬方法

[英]How to use JMock to test mocked methods inside a mocked method

我在確定如何模擬特定代碼方面遇到麻煩。

這是我的方法:

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

我希望能夠使用JMock來測試是否調用了findNotifications並返回了期望的值並調用了send()。

findNotifications稱我的Dao被嘲笑了。 通知是一個抽象類。

我有這樣的單元測試,但顯然不能正常工作。 它滿足前兩個期望,但僅此而已。

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

我怎樣才能使它按我的意願工作?

我嘗試過的另一種方式。 它滿足前兩個期望,但不滿足發送期望。

@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非常陌生,因此如果看起來不太了解我在做什么,我深表歉意(我沒有)。

在模擬方法中測試模擬方法。

您真的做不到。 不會進行real方法中的調用,因為您正在調用模擬而不是real方法。

您的第一次嘗試未通過最后兩個期望,因為使它們通過的調用僅在sendNotifications的實際實現中進行,而不是在您對它進行的模擬中進行。

第二個更接近於可行,但是您需要將您的mockedNotification notificationEntries添加到已設置為通過getByNotificationType模仿返回的notificationEntries列表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM