In this tutorial, we will learn how to use Spring boot provided @DataJpaTest annotation to test Repository layer components.
We use Spring boot starter test dependency to write JUnit tests for Repository layer components: <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
@DataJpaTest Annotation Overview
Spring Boot provides the @DataJpaTest annotation to test the persistence layer components that will autoconfigure in-memory embedded database for testing purposes.
The @DataJpaTest annotation doesn’t load other Spring beans (@Components, @Controller, @Service, and annotated beans) into ApplicationContext. By default, it scans for @Entity classes and configures Spring Data JPA repositories annotated with @Repository annotation.
By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test.
Spring boot provided @DataJpaTest annotation is not only to test Spring Data JPA repositories but it also supports to test JPA-related components.
For example:
@DataJpaTest Spring Boot Example
Create Spring Boot Application
- Spring Data JPA
- Lombok
- H2 Driver
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Create JPA Entity
package net.javaguides.spirngboot.entity;
import lombok.*;
import jakarta.persistence.*;
@Setter
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
private String email;
}
Create Spring Data JPA Repository
package net.javaguides.spirngboot.repository;
import net.javaguides.spirngboot.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
}
JUnit Tests for Data Access or Repository Layer with H2 database
package net.javaguides.spirngboot.repository;
import net.javaguides.spirngboot.AbstractContainerBaseTest;
import net.javaguides.spirngboot.entity.Student;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
class StudentRepositoryTest {
@Autowired
private StudentRepository studentRepository;
// JUnit for save student operation - BDD style
@Test
public void givenStudentObject_whenSave_thenReturnSavedStudent(){
// given - setup or precondition
Student student = Student.builder().firstName("Ramesh")
.lastName("fadatare").email("ramesh@gmail.com").build();
// when - action or the testing
Student savedStudent = studentRepository.save(student);
// then - very output
Assertions.assertNotNull(savedStudent);
Assertions.assertNotNull(savedStudent.getId());
}
// JUnit for findById student operation - BDD style
@Test
public void givenStudentId_whenfindbyId_thenReturnSavedStudent(){
// given - setup or precondition
Student student = Student.builder().firstName("Ramesh")
.lastName("fadatare").email("ramesh@gmail.com").build();
Student savedStudent = studentRepository.save(student);
// when - action or the testing
Student studentDB = studentRepository.findById(student.getId()).get();
// then - very output
Assertions.assertNotNull(studentDB);
}
}
Run JUnit tests
Conclusion
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment