Spring Boot Bootcamp – Workbooks and Challenges
)}

Challenge

Goal: Write tests to verify that your operations meet the expected requirements.

Launch the Starter Project

Screen Shot 2022-08-12 at 10.02.08 PM.png

Initial Remarks

  • In the testing section, you learned how to write unit tests with Mockito, and integration tests with MockMVC.

  • 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:

  1. Assert that the response returned is 200.
  2. Assert that the returned content is of type JSON.

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.

Screen Shot 2022-08-13 at 2.35.55 AM.png

The root element is simply the JSON document. So, $.name grabs the name of the root element.

Screen Shot 2022-08-13 at 2.05.10 AM.png


Using this information:

  1. Assert that the name returned by the JSON response equals the correct value. Feel free to see the Documentation for a working example.
  2. Assert that the phoneNumber returned by the JSON response equals the correct value.

Run your test:

Screen Shot 2022-08-13 at 2.10.30 AM.png

Test 2

Inside getAllContactsTest, mock a GET request to /contact/all.


Four assertions will follow:

  1. Assert that the response returned is 200.

  2. Assert that the returned content is of type JSON.


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:

  1. 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" } ]
  1. 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:

Screen Shot 2022-08-13 at 2.44.33 AM.png

Test 3

  • Inside contactNotFoundTest(), mock a GET request to /contact/4.
  • Assert that the response returned is 404.

Screen Shot 2022-08-13 at 2.47.45 AM.png

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).

Screen Shot 2022-08-13 at 3.09.04 AM.png

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.

Screen Shot 2022-08-13 at 3.13.23 AM.png

Congratulations!

By testing your API, you verified that every operation is working as it should.

Feedback Summary
5.0
3 students
5

100%
4

0%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.