Note that there are some explanatory texts on larger screens.

plurals
  1. POPowerMocking a new instantiation
    text
    copied!<p>In a class that I am testing with PowerMock I have the following instantiation of a class</p> <pre><code>EmailMessage msg = new EmailMessage(getExchangeSession()); </code></pre> <p><code>EmailMessage</code> is a third party tool that I am mocking while <code>getExchangeSession()</code> is a enherited protected method. I need to mock the <code>EmailMessage</code> but I really don't care about the call to <code>getExchangeSession()</code>.</p> <p>At the moment I have the following, but the <code>getExchangeSession()</code> method is still called:</p> <pre><code>@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) // ... } } </code></pre> <p>The first line of EmailProvider.send() is it that runs <code>getExchangeSession()</code> and which then fails.</p> <p>So apparently I can't skip the call to <code>getExchangeSession()</code>, 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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload