Posts

Showing posts with the label @DataJpaTest

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 - Testing a JPA application With @DataJpaTest - Example

Image
In this section, we will learn how to test Repository layer components with @DataJpaTest in Spring Boot application. 1.  @ 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 JPA tests. By default, it scans for @Entity classes and configures Spring Data JPA repositories. If an embedded database is available on the classpath, @DataJpaTest will autoconfigure one for testing purposes. By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test, means we do not need to clean up saved or modified table data after each test. Regular @Component , @Service or @Controller beans are not scanned when using this annotation.  This approach not only speeds up the testing process but also ensures a focused and efficient testing environment.  This approach is also known as "slicing" the application context. Find th