Skip to content

Latest commit

 

History

History
194 lines (158 loc) · 6.01 KB

File metadata and controls

194 lines (158 loc) · 6.01 KB

HOWTO: Spring Boot Library Application + Testing frameworks documentation

Overview

This document provides a detailed explanation of the different components of the Library Borrowing System built with Spring Boot.

Project Structure

The project follows the MVC (Model-View-Controller) pattern. Notices that the skeleton of the project can be generated with Sprint initializer.

In summary, the layers that are involved are:

  • Controller: Handles HTTP requests and sends responses.
  • Service: Contains business logic and interacts with repositories.
  • Repository: Manages data access and CRUD operations using Spring Data JPA.
  • Entity: Represents the data model and maps to a database table.

1. Model Layer

  • Located in src/main/java/com/example/library/model/
  • Defines JPA entities representing database tables.
  • Uses Hibernate annotations for ORM mapping.
  • Example:
    @Entity
    public class Book {
        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String title;
        private String author;
    }
  • Spring Data JPA Documentation

2. Repository Layer

  • Located in src/main/java/com/example/library/repository/
  • Interfaces extend JpaRepository to provide CRUD operations.
  • Example:
    public interface BookRepository extends JpaRepository<Book, Long> {}
  • Spring Data Repositories

3. Service Layer

  • Located in src/main/java/com/example/library/service/
  • Contains business logic.
  • Example:
    @Service
    public class BookService {
        private final BookRepository bookRepository;
        public List<Book> getAllBooks() { return bookRepository.findAll(); }
    }
  • Spring Service Layer

4. Controller Layer

  • Located in src/main/java/com/example/library/controller/
  • Handles HTTP requests and routes them to services.
  • Example:
    @RestController
    @RequestMapping("/books")
    public class BookController {
        private final BookService bookService;
        @GetMapping public List<Book> getBooks() { return bookService.getAllBooks(); }
    }
  • Spring MVC Controllers

5. View Layer

  • Located in src/main/resources/static/
  • Contains HTML, JavaScript, and CSS for the UI.
  • It could use Thymeleaf as the templating engine. However, it uses simple HTML with JavaScript code.
  • Example:
    <html>
    <body>
        <h1>Library System</h1>
        <script src="app.js"></script>
    </body>
    </html>
  • Thymeleaf Documentation

6. API Documentation with Swagger

  • Uses Springdoc OpenAPI for generating API documentation.
  • URL: http://localhost:8080/swagger-ui.html
  • Example Configuration:
    @OpenAPIDefinition(info = @Info(title = "Library API", version = "1.0"))
    @Configuration
    public class OpenAPIConfig {}
  • Springdoc OpenAPI

7. DTO - Data Transfer Objects

When building a REST API in Java using Spring Boot, it’s common to use Data Transfer Objects (DTOs) to encapsulate data that will be sent or received by the API. DTOs are simple objects that only contain fields and getter/setter methods, without any business logic, and are used to transfer data between layers of the application.

  • Example DTO:
    package com.example.demo.dto;
    
    public class ProductDTO {
    
        private Long id;
        private String name;
        private String description;
        private double price;
    
        // Constructors, getters, and setters
    
        public ProductDTO() {}
    
        public ProductDTO(Long id, String name, String description, double price) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.price = price;
        }
    
        // Getters and Setters
    }

Running the Application

  1. Start MySQL and create the database:
    mysql -u root -p < src/main/resources/dbsetup.sql
  2. Update application.properties with database credentials:
    spring.datasource.url=jdbc:mysql://localhost:3306/libraryapidb
    spring.datasource.username=root
    spring.datasource.password=password
  3. Start the application:
    mvn clean compile
    mvn spring-boot:run

Running Tests

  • Run all tests:

    mvn test
  • Run unit tests only:

    mvn -Dtest=UserServiceTest,BookServiceTest test
    mvn -Dtest=*ServiceTest test
  • Run integration tests:

    mvn verify
    mvn -Dtest=LibraryIntegrationTest test
  • Run performance tests:

    mvn -Dtest=PerformanceTest test
  • Run tests providing more details:

    mvn verify
    mvn test -Dspring-boot.run.profiles=test
  • Generate a test report (JaCoCo, PMD, Checkstyle):

    mvn site

Additional Resources