gpt4 book ai didi

java - 为 SendGrid 创建 JUnit 测试

转载 作者:行者123 更新时间:2023-11-30 05:30:04 26 4
gpt4 key购买 nike

我想创建 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)

最佳答案

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

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

关于java - 为 SendGrid 创建 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com