【问题标题】:Create JUnit test for SendGrid为 SendGrid 创建 JUnit 测试
【发布时间】:2019-09-02 11:04:06
【问题描述】:

我想创建 JUnit 测试来检查来自 SendGrid 的邮件。 我有 A 类,我有方法 send 有发送邮件的实现。:

public void send(Mail mail, String sendGridKey) throws IOException {
    SendGrid sg = new SendGrid(sendGridKey);
    Request request = new Request();

    try {
        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody(mail.build());
        Response response = sg.api(request);            
    } catch (IOException ex) {
        throw ex;
    }
}

我创建了 Junit 测试:

Assert.assertEquals( email.send(mail, SENDGRID_API_KEY), "");

我有这个错误:

The method assertEquals(Object, Object) in the type Assert is not applicable for the arguments (void, String)

【问题讨论】:

    标签: java spring-boot junit4 sendgrid


    【解决方案1】:

    assertEquals() 方法正在检查第一个参数(预期)和第二个参数(实际值)是否相等。在您的情况下,它将检查方法 email.send() 的返回参数是否与空的 String "" 相等。这不起作用,因为email.send() 是一个void 方法,因此没有返回值(例如String)。

    您可以将方法签名更改为 public String send(Mail mail, String sendGridKey) 或使用测试方法顶部的 @Test(expected = IOException.class) 来检查是否引发了异常(请参阅:exceptionThrownBaeldung

    【讨论】:

    • 好的,谢谢。你对我的问题有什么不同的方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多