模拟单元测试中的NullPointerException

时间:2013-11-08 16:07:11

标签: java nullpointerexception mockito

这是我正在尝试进行单元测试的SendEmail课程。我在下面的行上得到一个NullPointerException,但我不知道为什么。

另外,我是否正确组织了代码?我不知道我是否正确使用了mockito。

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();
    }
}

1 个答案:

答案 0 :(得分:3)

我认为你对你需要进行测试和嘲笑有点困惑。 Mockito提供了创建模拟对象的不同方法,例如:@MockMockito.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)
    }
}