Posts

Showing posts with the label Spring Data JPA

Spring Boot - Testing a JPA application with @DataJpaTest and Testcontainers

Image
In this section, we will learn how to test Repository layer components with @DataJpaTest and Testcontainers in JPA Spring Boot application that uses PostgreSQL as database . 1.  What we will build? We will create a basic JPA Spring Boot application that uses PostgreSQL as database. We will create Repository layer for this application. Finally we will do a testing with help of Testcontainers to verify our system is working as expected. 2. Testcontainers Testcontainers is an open source testing library that allows us to run docker containers directly in our spring boot application in order to facilitate integration tests with real dependencies.  It can provide instances of common databases(here PostgreSQL),  message brokers, Selenium web browsers, or anything else that can run in a Docker container. 3.  @ DataJpaTest Instead of bootstrapping the entire application context for every test,  @DataJpaTest  allows us to initialize only the parts of the Application context that are relevant to

Spring Boot PostgreSQL - Integration Testing with Testcontainers - Example

Image
In this section, we will learn  how to test Spring Boot, Spring Data JPA, and PostgreSQL based application using Testcontainers. 1.  What we will build? We will create a web application with Spring Boot, Spring Data JPA and PostgreSQL database. The application will consist of three layer,  ie. a Controller, a Service, and a Repository layer.  Controller layer takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON response. Service layer  facilitates communication between the controller and the repository layer. We are using Spring Data JPA for managing database operations, so we can use Spring Data JPARepository interface. **Finally we will do a integration testing with help of Testcontainers to verify our system is working as expected. 2. Testcontainers Testcontainers is an open source testing library that allows us to run docker containers directly in our spring boot application in orde