[英]NullPointerException in mockito unit test
這是我要進行單元測試的SendEmail
類。 我在下面顯示的行上收到NullPointerException,但是我不知道為什么。
另外,我是否正確組織了代碼? 我不確定我是否正確使用了模仿。
public class StatsTest extends AbstractTestCase {
@Mock
MultiPartEmail MultiPartEmailMock;
StatsTest statsTestObj;
SendEmail mockedEmail;
@Before
public void setUp() throws Throwable {
super.setUp();
MockitoAnnotations.initMocks(this);
}
@Test(expected = ValidationException.class)
public void testSendEmail() throws EmailException, IOException {
MultiPartEmail multiPartEmailMock;
SendEmail mockedEmail = Mockito.mock(SendEmail.class);
Mockito.when((mockedEmail.getHtmlEmail()).send())
.thenThrow(new ValidationException("Could not send the Email."));
^^ the line above is where the null pointer error is
mockedEmail.sendEmail();
}
}
這是要測試的課程:
public class SendEmail {
private StringBuilder emailBody;
private String senderEmail;
private ArrayList<String> receiversEmails;
private String emailSubject;
private String hostName;
private MultiPartEmail htmlEmail;
public SendEmail(StringBuilder emailBody, String senderEmail, ArrayList<String>
receiversEmails, String emailSubject, String hostName, MultiPartEmail htmlEmail) {
this.setEmailBody(emailBody);
this.setSenderEmail(senderEmail);
this.setReceiversEmails(receiversEmails);
this.setEmailSubject(emailSubject);
this.setHostName(hostName);
this.setHtmlEmail(htmlEmail);
}
public void sendEmail()throws IOException, EmailException{
htmlEmail.setHostName(getHostName());
htmlEmail.addTo(getReceiversEmails().get(0));
for(int i=0; i<getReceiversEmails().size(); i++){
htmlEmail.addCc(getReceiversEmails().get(i));
}
htmlEmail.setFrom(getSenderEmail());
htmlEmail.setSubject(getEmailSubject());
htmlEmail.setMsg((getEmailBody()).toString());
htmlEmail.send();
}
}
我認為您對需要測試和模擬的內容有些困惑。 Mockito提供了創建模擬對象的不同方法,例如: @Mock
或Mockito.mock()
。 還有多種方法可以將這些模擬對象注入到要測試的類中,以便對該類進行單元測試。
如果沒有異常或其他類的所有詳細信息,這將不是一個完整的答案,但是我希望它可以幫助您清除一些概念。
注意:以下內容可能無法編譯,但希望您能理解。
public class StatsTest extends AbstractTestCase {
@Mock MultiPartEmail mockMultiPartEmail; // this mock will be used in the instantiaion of the class under test
SendEmail sendEmail; // renamed as this is not a mock, it is the class under test
// define some things we can make assertions against (probably in some other tests...)
private static final StringBuilder BODY = new StringBuilder("body");
private static final String SENDER = "sender@foo.com";
private static final Collection<String> RECIPIENTS = Arrays.asList("recepient@foo.com")
private static final String SUBJECT = "subject";
private static final String HOSTNAME = "hostname";
@Before
public void setUp() throws Throwable {
super.setUp();
MockitoAnnotations.initMocks(this); // will instantiate "mockMultiPartEmail"
// instantiate our class under test
sendEmail = new SendEmail(BODY, SENDER, RECIPIENTS, SUBJECT, HOSTNAME, mockMultiPartEmail);
}
@Test(expected = ValidationException.class)
public void testSendEmail() throws EmailException, IOException {
// given some condition
Mockito.when(mockMultiPartEmail.send()).thenThrow(new ValidationException("Could not send the Email."));
// when the method under test is called
sendEmail.sendEmail();
// then the exception will be thrown (and you have correctly expected this on the @Test annotation)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.