Search code examples
javaspring-bootjunit4sendgrid

Create JUnit test for SendGrid


I want to create the JUnit test for checking mail from SendGrid. I have class A and there I have the method send who have implementation for sending mail.:

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

And I created the Junit test :

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

and I have this error:

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

Solution

  • The assertEquals() method is checking the first parameter (expected) and the second parameter (actual value) for equality. In your case it will check the return parameter of the method email.send() for equality with an empty String "". This will not work, because the email.send() is a void method and therefore has no return value (such as String).

    You could either change the method signature to public String send(Mail mail, String sendGridKey) or work with the @Test(expected = IOException.class) on top of your test method to check if an exception was thrown (see: exceptionThrownBaeldung)