testing the controller
Last updated
Last updated
The function under test
The controller receives POST requests from clients and hands them off to the 'hendler' function. It receives a Response object from the handler and passes it back to the client as JSON.
Possible Inputs
A call to the function has the following components:
The path ( e.g. "/email/v2/send"\
)
The method (e.g. POST
)
A json
request body having a simplified email request
The path can be correct or incorrect
The method can be POST or an unsupported method such as GET, PUT, etc.
The JSON string can be valid or invalid
Possible outputs
a Sendgrid Response object
a Runtime exception
a null Response object
First, we want to test how the controller responds to good and bad inputs. In this is case we can establish a pattern in one unt test and then simply vary the data to extend it to other cases. That's the case with the client input.
We can often come up with a single pattern for several units test and vary the inputs to create our various scenarios. In this case we'll:
create a MockMvc call to the controller - this mimics a real call to the /send endpoint
set the content that we want to test for
expect the correct response for that content
We'll use this pattern to test:
Bad json format (Something that will fail json conversion)
Bad path (the client is calling a path that we do not support)
Bad method (something other than POST)
The controller receives outputs from the SendGrid handler. To test that, we'll use the same pattern as above, but we need to mock the requestHandler so that we can get the return values we want to test.
We'll use this pattern to test: 1. Any return code Handle any return code we get from the SendgridHandler 2. Null return Handle a null returning from the SendgridHandler 3. Exceptions Handle a runtime exception being thrown from the SendgridHandler