Contenido del Curso
Spring Boot Backend
Spring Boot Backend
Challenge: Unit Testing
Task
We’re nearing the end of this course, and to wrap up our application, I propose you finish writing all the unit tests for the controller and service layers.
Task Explanation
You need to test all the methods in both the controller and service layers that don’t yet have tests written for them. For this, I recommend using JUnit
along with the Mockito
library.
Test Writing Guidelines for JUnit
Here are some key methods you'll find useful:
assertNotNull(result)
: Ensures that the result of a method call is not null
, confirming the operation was successful;
assertEquals(expected, actual)
: Compares the expected value with the actual value to verify that the correct data is returned;
assertTrue(condition)
: Checks that a condition is true, for example, ensuring a list of books is empty if there are no records in the database;
verify(mock).methodCall()
: Verifies that a specific method on a mock object was called with the expected parameters, validating the interaction between components;
when(mock.methodCall()).thenReturn(value)
: Configures a mock to return a certain value when a method is called, simulating dependencies in your tests.
Controller Testing
Here, tests have already been written for the methods findAllBooks
, findByAuthor
, createBook
, and updateBook
. You'll need to write tests for the remaining method. Below are some tips to help you implement the test for this method.
deleteBooks(String id)
For the deleteBooks(String id)
method, you need to verify that the delete method is called with the correct id
and test error handling when trying to delete a non-existent book. For a successful response, check only the status; for errors, verify both the status and error message.
BookService
@Transactional public void deleteBook(String id) { if(!bookRepository.existsById(id)) { throw new ApiException("Not found by id: " + id, HttpStatus.NOT_FOUND); } bookRepository.deleteById(id); }
For the controller, you will need to write two tests for this method: one for the successful deletion and another for when a 404 status
is returned.
Keep in mind that the service method we will be mocking returns nothing, just like the controller.
You can use the doNothing()
method to stub a void
method, ensuring no actions are performed upon its invocation. The syntax is:
When you need to handle a scenario where an exception is thrown, use doThrow()
. This method in Mockito
simulates throwing an exception from a void
method. The syntax is:
Service Testing
deleteBook(String id)
The deleteBook(String id)
method requires validation to ensure that the book deletion method is called with the correct id
. Additionally, it is essential to test how the method handles the situation when a book with the specified id
is not found.
You will need to implement two tests: one where the entity is found by id
and deleted, and another where an exception is thrown.
What If You Can’t Write Tests?
It’s completely okay if you didn’t grasp the topic on your first attempt; you might need to read through the testing concepts again. I also recommend reviewing the solution to this assignment.
¡Gracias por tus comentarios!