I have a function that processes events obtained by a PullSubscription
to Microsoft 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);
}
}
}
I am trying to test it using PowerMockito because ExchangeService
is a final class.
So I have mocked ExchangeService
and PullSubscription
as follows:
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);
}
When I'm trying to test it using the following code it throws a NullPointerException
because subscription.getEvents()
returns null
(i.e. the subscriptionMock
has no events in it).
I tried stubbing it by mocking the eventResults
that has to be returned:
when(subscriptionMock.getEvents()).thenReturn(eventResultsMock);
It doesn't work since the getEvents()
is not called in the test function. I wanted to know how to test this function?
http://archive.msdn.microsoft.com/ewsjavaapi
Solution:
I had to mock every object being created in the function. Also, I had to add the following above the class declaration.
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ClassBeingTested.class })