I had the fortune of growing up in a house full of books, and several times while growing up, I wanted to organize and arrange them in some way to make it easier to find them and know what books were there. After finishing university, I tried to do it with a couple of applications, but none of them fully convinced me. So, as soon as I started learning programming, I had the idea in my mind to create my own application.
And it all begins here:
The code provided is a basic Spring Boot application with RESTful API functionality to manage a home library and includes both integration and unit testing.
Here’s what the code does summarized:
- Libro.java (The Book Details): it describes each book. For every book, you can note down its name, author, and where it is in the library.
- LibroRepository.java (The Bookkeeper): This part keeps track of all your books. It knows how to save, find, and delete books.
- LibroController.java (The Book Manager): It helps you manage your books. It understands commands like “Show me all the books” or “Add a new book.”
- BibliotecaApplication.java (The Starter): This starts the whole library system. It’s where the adventure begins.
- Tests (The Checkers): There are little checkers to make sure everything is working correctly. They’re like friends who make sure the tool does what it’s supposed to do.
Project Structure
The project is structured as a Spring Boot application.
- The main code is located in the
src/main/java/net/Karvala/Bibliotecadirectory. - The code includes a RESTful controller (
LibroController), an entity class (Libro), a repository interface (LibroRepository), and the main application class (BibliotecaApplication). - Tests are provided in the
src/testdirectory.
Libro Entity (Libro.java)
Represents a book with attributes such as title, author, and shelf.
- Annotated with
@Entityto indicate it is a JPA entity. - Uses
@Idand@GeneratedValueannotations for primary key generation.
Libro Repository (LibroRepository.java)
Extends JpaRepository, providing CRUD operations for the Libro entity.
Libro Controller (LibroController.java)
Implements CRUD operations for managing books using Spring Web’s RESTful capabilities.
- Uses dependency injection to inject a
LibroRepositoryinstance. - Defines methods for listing all books, finding a book by ID, creating a new book, updating a book, and deleting a book.
Biblioteca Application (BibliotecaApplication.java)
The main class that runs the Spring Boot application.
- Demonstrates the usage of the
LibroRepositoryby creating, saving, and retrieving books
Tests
LibroControllerTest.javaperforms integration tests for theLibroController.
LibroControllerUnitTest.javauses Mockito to perform unit tests for theLibroController.
Code Highlights
- Dependency Injection: Constructor injection is used in
LibroControllerto inject theLibroRepositoryinstance. - RESTful Endpoints: CRUD operations are implemented using Spring’s annotations (
@GetMapping,@PostMapping,@PutMapping,@DeleteMapping). - Response Handling: Utilizes
ResponseEntityfor more control over HTTP responses. - Testing: Integration tests (
LibroControllerTest.java) useTestRestTemplateto test the actual HTTP endpoints. Unit tests (LibroControllerUnitTest.java) use Mockito for mocking the repository and testing controller methods independently.
How It Works
- Entity and Repository:
Libroclass represents a book, andLibroRepositoryis an interface for CRUD operations on books. - Controller (
LibroController.java):- Handles HTTP requests related to books.
- Uses
LibroRepositoryfor database operations. - Defines endpoints for finding all books, finding a book by id, creating, updating, and deleting books.
- Application (
BibliotecaApplication.java): Initializes the Spring Boot application.- Injects the
LibroRepository. - Performs sample CRUD operations on books.
- Injects the
- Tests:
LibroControllerTest.java: Integration tests usingTestRestTemplate.LibroControllerUnitTest.java: Unit tests using Mockito and MockMvc.





