Welcome to myBookStore! myBookStore is a command-line application that enables users to create an account, log in, and purchase and sell books. I built this application to demonstrate what I have learned so far in Spring.
Spring Boot for simplifying Spring configuration
- myBookstore uses Spring Boot to simplify Spring configuration and dependency management.
Spring Data, Hibernate, and MySQL for data persistence and Object-Relational Mapping
- myBookstore handles data persistence by employing Spring Data JPA and Hibernate, mapping Java objects to a relational MySQL database and executing seamless CRUD operations without the use of raw SQL queries.
JUnit and Mockito for unit and integration testing
- myBookstore utilizes JUnit 5 and Mockito to thoroughly test the application's logic and database interactions. JUnit 5 is used for unit testing, as well as integration testing with Spring Data JPA. Mockito is used for mocking dependencies and isolating testing of service-level classes.
myBookstore is implemented using N-tier architecture. The data access layer is abstracted through Data Access Object interfaces, and business logic is handled by service classes. Finally, UI classes within the presentation layer handle all user interactions and send requests to the logic layer for processing. Altogether, this design promotes modularity, security, and maintainability.
In designing and building this project, I learned the following:
- The purpose of Inversion of Control (IoC) and Dependency Injection (DI). Spring follows the principle of IoC, and one pattern of IoC that it uses is DI. By transferring the responsibility of an object's lifecycle to the Spring IoC container, the application becomes loosely coupled. The code therefore becomes more modular and testable; developers can reuse components in different contexts and test them in isolation via mocks.
- Setting up a Spring Boot CRUD application. With the help of Chad Darby's Spring Boot course on Udemy, I learned how to connect to a MySQL database, create and configure entity classes and Data Access Objects, and implement JPA CRUD operations.
- Testing classes in the data access layer. I learned how to use the
@DataJpaTestannotation to create an embedded in-memory (H2) database and perform transactional operations that were automatically rolled back after execution. Moreover, I learned how to use the@Importannotation to load custom repository implementations, since@DataJpaTestonly scans for Spring Data JPA repositories. - Testing logic-level classes in isolation using the Mockito framework. I learned the importance of using mock dependencies to test business logic code in a controlled environment (i.e., without interference from data access dependencies).
- Spring Annotations:
- Fundamental annotations such as
@SpringBootApplication,@Component, and@Autowired - Functionality-related annotations such as
@Service,@Repository, and@Transactional - JPA mapping annotations such as
@Entity,@Table,@GeneratedValue, and@Column - Test-related annotations such as
@DataJpaTest,@Mock, and@InjectMocks
- Fundamental annotations such as
Christal Dita | LinkedIn
I used the following resources to guide my learning:
- Chad Darby's Spring Boot 3, Spring 6 & Hibernate for Beginners course on Udemy
- What is Java Spring Boot? on IBM
- Using the @SpringBootApplication Annotation on Spring docs
- Difference between controller, service, and manager by Patricio Garcia on Medium
- Spring Boot - Database Integration (JPA, Hibernate, MySQL, H2) by Mahesh K on GeeksforGeeks
- Using @Autowired and @InjectMocks in Spring Boot Tests by Abhinav Pandey on Baeldung
- Mockito.mock() vs @Mock vs @MockBean by baeldung on Baeldung
- @DataJpaTest and Repository Class in JUnit by Wynn Teo on Baeldung
- Annotation Interface DataJpaTest on Spring docs
- Spring @Import Annotation by Gilvan Ornelas on Baeldung
- Annotation Interface Repository on Spring docs
- Spring @Service Annotation by Pankaj Kumar on DigitalOcean