🐜
tiny engines
  • Tiny Engines
  • Personal Website Home
  • NFL machine learning capstone
    • project presentation
    • project proposal
    • project approach
    • project structure
    • project workflow
    • project summary
    • project code
  • Onboarding new hires
    • motivation
    • the project
    • the mailer service
      • mailer setup
      • walk-through
      • unit testing
      • testing the controller
      • testing the handler
      • testing the mailer
      • integration testing
      • integration example
      • acceptance testing
      • acceptance example
      • documenting the API
      • test coverage
      • performance testing
      • mutation testing
      • grammar checking
    • the event listener
      • design
      • webhook setup
      • walk-through
      • testing
      • the kafka connector
  • Walk-throughs
    • spark streaming hld
      • background
      • architecture
      • threat
      • project
      • transform-design
      • transform-poc
      • query-poc
    • kafka walkthroughs
    • java futures
      • async servers
      • async clients
      • async streams
Powered by GitBook
On this page
  • Example - Testing the Controller request
  • Example - Testing the SendgridMailer response

Was this helpful?

  1. Onboarding new hires
  2. the mailer service

testing the handler

Previoustesting the controllerNexttesting the mailer

Last updated 3 years ago

Was this helpful?

The function under test

Possible inputs

  • The json can be correct or incorrect

  • A "good" json string is converted to a SendgridRequest object

  • A failed conversion is snet back to the client as a BAD REQUEST

Possible outputs

  • a Sendgrid Response object

  • a Runtime exception

  • a null Response object

Example - Testing the Controller request

We don't have many scenarios to cover in the handler. The json conversion will either fail or succeed for any number of reasons. If the conversion succeeds we pass the result to the SendgridMailer function. If it fails we want to return a BAD REQUEST.

In the example below, the handler receives a bad json string which will throw a Json exception. The handler function should catch the exception and return a BAD_REQUEST Response object to the controller

@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());
}

Example - Testing the SendgridMailer response

In the example below, the handler calls the mailer function which throws an exception. The handler function should catch the exception and return a Response object to the controller

example - handle a mailer exception
@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())
    );
}