在,我與PowerMock測試類我有一個類PowerMocking一個新的實例
EmailMessage msg = new EmailMessage(getExchangeSession());
EmailMessage
以下實例是一個第三方工具,我嘲笑而getExchangeSession()
是enherited保護的方法。我需要嘲笑EmailMessage
,但我真的不在意撥打getExchangeSession()
的電話。
目前我有以下,但getExchangeSession()
方法仍稱:
@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)
// ...
}
}
EmailProvider.send的第一行()是它運行getExchangeSession()
和再失敗。
所以顯然我不能跳過對getExchangeSession()
的電話,我應該也可以嘲笑這種方法。它是否正確? 如果是這樣,在EasyMock中使用PowerMock,我該如何嘲笑這種受保護的方法?
你是否將這些添加到了測試中? @RunWith(PowerMockRunner.class) @PrepareForTest(Test.class)。你也可以查看這個鏈接使用powermock.expectprivate,它可能也適用於受保護的方法(我使用justmock自己所以不是100%確定)編輯(添加鏈接):http://stackoverflow.com/questions/17027582/powermock -easymock-private-void-method-without-invokation – Josh