In a class that I am testing with PowerMock I have the following instantiation of a class
EmailMessage msg = new EmailMessage(getExchangeSession());
EmailMessage
is a third party tool that I am mocking while getExchangeSession()
is a enherited protected method. I need to mock the EmailMessage
but I really don't care about the call to getExchangeSession()
.
At the moment I have the following, but the getExchangeSession()
method is still called:
@RunWith(PowerMockRunner.class)
@PrepareForTest({EmailProvider.class, ExchangeService.class})
public class MyTest {
@Test
public void test() {
EmailMessage emailMessage = createMock(EmailMessage.class);
ExchangeService exchangeService = createMock(ExchangeService.class);
expectNew(EmailMessage.class, exchangeService).andReturn(emailMessage);
// test setup and call to the class under test
Email email = new Email();
new EmailProvider().send(email);
}
}
public class EmailProvider() extends ClassWithProtectedAccess {
public void send(Email email) {
EmailMessage msg = new EmailMessage(getExchangeSession());
// and here follows the code that I am actually testing
// and which works on the msg (EmailMessage)
// ...
}
}
The first line of EmailProvider.send() is it that runs getExchangeSession()
and which then fails.
So apparently I can't skip the call to getExchangeSession()
, and I should probably mock this method also. Is this correct?
And if so, using PowerMock with EasyMock how can I mock this protected method?