This project is a Java application based on Spring Boot that provides CRUD (Create, Read, Update, Delete) services for managing users. The service uses an in-memory list to store users, with functionalities to save, search, and delete users. Additionally, it includes exception handling for scenarios such as creating duplicate users and searching for non-existing users.
- Create user: Allows saving a new user with a unique ID.
- Find users: Allows fetching all saved users or searching by name or ID.
- Delete user: Allows deleting a user by their ID.
- Exceptions: The application throws custom exceptions (
UserAlreadyExistsException
andUserNotFoundException
) for errors related to user existence.
To run the application, the following are required:
- Java 17 (or higher).
- Maven to manage dependencies and build the project.
This project uses the following main dependencies:
- Spring Boot: A framework for building Java applications.
- Lombok: A library to reduce repetitive code, especially in POJOs (Plain Old Java Objects).
- JUnit 5: A framework for unit testing.
- Spring Boot Starter Test: Testing support for Spring Boot applications.
-
Clone this repository to your local machine:
git clone https://gitlab.nfqsolutions.es/andres.liebana/andres.liebana-springboot-advanced-testing.git
-
Navigate to the project directory:
cd examen-bloque-8
-
Build the project with Maven:
mvn clean install
To start the application, use the following command:
mvn spring-boot:run
This will start the Spring Boot server on the default port (8080
).
This project includes unit tests to verify the main functionalities of the user service.
To run the tests, use the following command:
mvn test
The basic project structure is as follows:
andres.liebana-springboot-advanced-testing/
│
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/
│ │ └── nter/
│ │ └── evaluanter/
│ │ ├── UserServiceImpl.java
│ │ ├── UserEntity.java
│ │ ├── Address.java
│ │ ├── UserAlreadyExistsException.java
│ │ └── UserNotFoundException.java
│ ├── test/
│ │ └── java/
│ │ └── com/
│ │ └── nter/
│ │ └── evaluanter/
│ │ └── UserServiceImplTest.java
│
├── pom.xml
└── README.md
- UserAlreadyExistsException: Thrown when trying to create a user with an already existing name.
- UserNotFoundException: Thrown when a user with the provided ID cannot be found.
Tests are defined in UserServiceImplTest.java
and cover the following scenarios:
- Successfully saving a user.
- Attempting to save a user that already exists (should throw
UserAlreadyExistsException
). - Finding all users.
- Finding a user by name.
- Finding a user by ID (and throwing
UserNotFoundException
when not found). - Deleting a user by ID (and throwing
UserNotFoundException
when not found).
Some lines and methods are intentionally excluded from test coverage, mainly those related to UserEntity
and Address
classes. These classes are simple data holders and rely on Lombok annotations (@Getter
, @Setter
, @Builder
, etc.) to generate boilerplate code like getters, setters, and constructors.
Since this code is auto-generated and contains no business logic, writing unit tests for these methods would not add value and would only artificially inflate the coverage percentage. The focus of testing is on the service layer (UserServiceImpl
), where real logic and business rules are implemented and validated through a complete set of tests.
In summary: getters and setters are not tested because they are auto-generated and contain no custom logic, so there's no meaningful value in testing them.