1

我有一个处理由PullSubscriptionMicrosoft Exchange 获得的事件的功能。

public void processEvents(ExchangeService service, PullSubscription subscription)
            throws Exception {
    GetEventsResults events = subscription.getEvents();
    // Loop through all item-related events.
    for (ItemEvent itemEvent : events.getItemEvents()) {
        if (itemEvent.getEventType() == EventType.NewMail) {
            EmailMessage message = EmailMessage.bind(service, itemEvent.getItemId());
            EmailParser emailParser = new EmailParser();
            emailParser.parse(message, service);
        }
    }
}

我正在尝试使用 PowerMockito 对其进行测试,因为ExchangeService它是最后一堂课。所以我嘲笑ExchangeServicePullSubscription如下:

ExchangeService serviceMock = PowerMockito.mock(ExchangeService.class);
PullSubscription subscriptionMock = PowerMockito.mock(PullSubscription.class);

@Test
public void startPullNotification() throws Exception {
    ProcessEmails pr = new ProcessEmails("config.properties");
    pr.startPullNotification(serviceMock);
}

当我尝试使用以下代码对其进行测试时,它会抛出一个NullPointerException因为subscription.getEvents()返回null(即其中subscriptionMock没有事件)。

eventResults我尝试通过模拟必须返回的来存根它:

when(subscriptionMock.getEvents()).thenReturn(eventResultsMock);

它不起作用,因为getEvents()在测试函数中没有调用。我想知道如何测试这个功能?

http://archive.msdn.microsoft.com/ewsjavaapi

解决方案:

我不得不模拟函数中创建的每个对象。另外,我必须在类声明上方添加以下内容。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ClassBeingTested.class })
4

0 回答 0