为 SendGrid 创建 JUnit 测试
Create JUnit test for SendGrid
我想创建 JUnit 测试来检查来自 SendGrid 的邮件。
我有 class 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()
的 return 参数是否与空 String
""
相等。这将不起作用,因为 email.send()
是一个 void 方法,因此没有 return 值(例如 String
)。
您可以将方法签名更改为 public String send(Mail mail, String sendGridKey)
或在测试方法之上使用 @Test(expected = IOException.class)
来检查是否抛出了异常(请参阅:exceptionThrownBaeldung)
我想创建 JUnit 测试来检查来自 SendGrid 的邮件。
我有 class 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()
的 return 参数是否与空 String
""
相等。这将不起作用,因为 email.send()
是一个 void 方法,因此没有 return 值(例如 String
)。
您可以将方法签名更改为 public String send(Mail mail, String sendGridKey)
或在测试方法之上使用 @Test(expected = IOException.class)
来检查是否抛出了异常(请参阅:exceptionThrownBaeldung)