Challenge
Goal: Write tests to verify that your operations meet the expected requirements.
Launch the Starter Project
Initial Remarks
-
In the testing section, you learned how to write unit tests with
Mockito
, and integration tests withMockMVC
. -
The
Service
layer doesn't contain meaningful logic. So, we will not write any unit tests.
Starter Project
The starter project includes two methods.
setup()
will run @BeforeEach
integration test and populate the repository with data.
@BeforeEach
void setup(){
for (int i = 0; i < contacts.length; i++) {
contactRepository.saveContact(contacts[i]);
}
}
clear()
will run @AfterEach
integration test and clear the repository.
@AfterEach
void clear(){
contactRepository.getContacts().clear();
}
Cheat Sheet
For the tasks that follow, feel free to use the cheat sheet from the Testing section.
Test 1
Inside getContactByIdTest
, mock a GET request to /contact/1
.
Four assertions will follow:
- Assert that the response returned is 200.
- Hint: see the Documentation for
andExpect()
.
- Hint: see the Documentation for
- Assert that the returned content is of type
JSON
.- Hint: see the Documentation for
andExpect()
.
- Hint: see the Documentation for
Side Note: JsonPath
JSONPath
allows you to query JSON.
Assume that you have the following JSON structure:
{ "id": "971aa9ff-bdcc-4569-833a-87bb4d383a2d", "name": "Rayan", "phoneNumber": "5334221234" }
Given the following query jsonPath("$.name")
, the dollar sign refers to the root element.
The root element is simply the JSON document. So, $.name
grabs the name of the root element.
Using this information:
- Assert that the
name
returned by the JSON response equals the correct value. Feel free to see the Documentation for a working example. - Assert that the
phoneNumber
returned by the JSON response equals the correct value.
Run your test:
Test 2
Inside getAllContactsTest
, mock a GET request to /contact/all
.
Four assertions will follow:
-
Assert that the response returned is 200.
- Hint: see the Documentation for
andExpect()
.
- Hint: see the Documentation for
-
Assert that the returned content is of type
JSON
.- Hint: see the Documentation for
andExpect()
.
- Hint: see the Documentation for
Side Note: JsonPath
Assume that you have the following JSON Array:
[ { "name": "Jon Snow", "age": 25 }, { "name": "Tyrion Lannister", "age": 40 }, { "name": "The Hound", "age": 40 } ]
Because the root element is an Array, you can use $.size()
to count the number of documents.
Using this information:
- Assert that the size of the returned JSON collection is correct.
Side Note: JsonPath
The following query checks for the existence of a JSON document with:
- an age of 25.
- a name of Jon Snow.
jsonPath("$.[?(@.age == 25 && @.name == \"Jon Snow\")]").exists()
Final Assertion
We expect getContacts()
to return the following JSON:
[ { "id": "1", "name": "Jon Snow", "phoneNumber": "6135342524" }, { "id": "2", "name": "Tyrion Lannister", "phoneNumber": "4145433332" }, { "id": "3", "name": "The Hound", "phoneNumber": "3452125631" } ]
-
From the JSON collection that gets returned, make an assertion that checks for the existence of a document with:
id
="2"
name
="Tyrion Lannister"
phoneNumber
="4145433332"
.
Run your test:
Test 3
- Inside
contactNotFoundTest()
, mock a GET request to/contact/4
. - Assert that the response returned is 404.
ObjectMapper
Can convert any POJO to JSON.
For test 4, you will need to autowire the ObjectMapper
bean inside your test class.
Test 4
Inside validContactCreation()
, mock a POST request to /contact
.
Assume that you want to send this JSON as part of the request body:
{ "name": "Jon", "age": "25" }
Inside your request builder, you can specify that the type of content you'll be sending is JSON.
.contentType(MediaType.APPLICATION_JSON)
Inside your request builder, you can use objectMapper
to serialize a Java Object into a JSON
String.
.content(objectMapper.writeValueAsString(new Student("Jon", 25)));
Using this information:
As you perform the request, send the following JSON as payload.
{ "id": null, "name": "Rayan", "phoneNumber": "5334221234" }
Assert that the response returned is 201 (Created).
Test 5
- Inside
invalidContactCreation()
, mock a POST request to/contact
. - Send the following JSON as your POST request body.
{ "id": null, "name": " ", "phoneNumber": " " }
- Assert that the response returned is 400.
Congratulations!
By testing your API, you verified that every operation is working as it should.