testing the handler
Last updated
Last updated
@Test
void test_handle_bad_json_input_returns_400() throws Exception {
// when the handler's requestHandler() method is called with bad json
var response = new SendgridHandler(new SendgridMailer()).requestHandler("{ \"name\":\"jo}");
// then it should return a BAD REQUEST Response object
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value());
}@Test
void handles_requestHandler_exception() throws Exception {
// when the handler calls the mailer and it throws an exception
var sendGridMailer = mock(SendgridMailer.class);
when(sendGridMailer.send(any(SendgridRequest.class))).thenThrow(new RuntimeException("Badd JuJu"));
var response = new SendgridHandler(sendGridMailer).requestHandler(createRequest());
// then the handler will return a valid Respose object
assertAll(
() -> assertEquals(response.getStatusCode(), HttpStatus.INTERNAL_SERVER_ERROR.value()),
() -> assertEquals("Badd JuJu", response.getBody())
);
}