0% found this document useful (0 votes)
36 views

What Is @DataJpaTestAnnotations and Spring Boot Unit Testing Repository Layer

The document provides code examples for writing JUnit tests to test various operations of a Spring Data JPA employee repository, including saving an employee, finding all employees, finding by ID, finding by email, and updating an employee.

Uploaded by

Sourabh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

What Is @DataJpaTestAnnotations and Spring Boot Unit Testing Repository Layer

The document provides code examples for writing JUnit tests to test various operations of a Spring Data JPA employee repository, including saving an employee, finding all employees, finding by ID, finding by email, and updating an employee.

Uploaded by

Sourabh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Sometimes we have requirements to test only the repository layer component we are using @DataJpaTest

Annotations.

So it will load the @DataJpaTest Annotations.


Now we are going to write the unit test for save employee operations.

Here we need to create a test class

All the test class we are going to keep here in test package like this:
Also add Lombok plugin in you eclipse like this:

Link to be used : https://projectlombok.org/p2


Now creating a EmployeeRepositoryTests class here in Repository test package like this:

Unit test for save employee operation

package net.javaguides.springboot.repository;
import net.javaguides.springboot.model.Employee;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.api.Assertions;

import org.junit.jupiter.api.DisplayName;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest

public class EmployeeRepositoryTests {

// inject the employee repository here

@Autowired

private EmployeeRepository employeeRepository;

// Junit test for save employee operations


@DisplayName("Juint test for save employee operation")

@Test

public void givenEmployeeObject_whenSave_thenReturnSavedEmployee() {

//given - precondition or setup

Employee employee= Employee.builder()

.firstName("Ramesh")

.lastName("Ramesh")

.email("ramesh@gmail,com")

.build();

//when- action or the behavior that we are going to test

Employee savedEmployee = employeeRepository.save(employee);

//then - verify the output

Assertions.assertThat(savedEmployee).isNotNull();

Assertions.assertThat(savedEmployee.getId()).isGreaterThan(0);
}

Unit test for get all employees operations


Edit the EmployeeRepositoryTests class here in Repository test package like this:

package net.javaguides.springboot.repository;
import net.javaguides.springboot.model.Employee;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.assertj.core.api.Assertions;

import org.junit.jupiter.api.DisplayName;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest

public class EmployeeRepositoryTests {

// inject the employee repository here

@Autowired

private EmployeeRepository employeeRepository;

// Junit test for save employee operations

@DisplayName("Juint test for save employee operation")

@Test

public void givenEmployeeObject_whenSave_thenReturnSavedEmployee() {

//given - precondition or setup

Employee employee= Employee.builder()

.firstName("Ramesh")

.lastName("Ramesh")

.email("ramesh@gmail,com")
.build();

//when- action or the behavior that we are going to test

Employee savedEmployee = employeeRepository.save(employee);

//then - verify the output

assertThat(savedEmployee).isNotNull();

assertThat(savedEmployee.getId()).isGreaterThan(0);

//Junit Test for get all employees operations

@DisplayName("Junit test for get all employees operation")

@Test

public void givenEmployeesList_whenFindAll_thenEmployeesList() {

//given - precondition or setup

Employee employee= Employee.builder()

.firstName("Ramesh")

.lastName("Ramesh")

.email("ramesh@gmail,com")

.build();

Employee employee1= Employee.builder()

.firstName("Jhon")

.lastName("Cena")

.email("Cena@gmail,com")

.build();

employeeRepository.save(employee);

employeeRepository.save(employee1);

//when- action or the behavior that we are going to test

List<Employee> emplyeeList = employeeRepository.findAll();


//then - verify the output

assertThat(emplyeeList).isNotNull();

assertThat(emplyeeList.size()).isEqualTo(2);

And out put is:


Unit test for get employee by id operation

We have to add below code in the EmployeeRepositoryTests class


// JUnit test for get employee by id operation
@DisplayName("JUnit test for get employee by id operation")
@Test
public void givenEmployeeObject_whenFindById_thenReturnEmployeeObject(){
// given - precondition or setup
Employee employee = Employee.builder()
.firstName("Ramesh")
.lastName("Ramesh")
.email("ramesh@gmail,com")
.build();
employeeRepository.save(employee);

// when - action or the behaviour that we are going test


Employee employeeDB = employeeRepository.findById(employee.getId()).get();

// then - verify the output


assertThat(employeeDB).isNotNull();
}

Following as the output we will get here:


Unit test for get employee by email operation (Spring Data JPA query method)

First edit the EmployeeRepository class here like this

package net.javaguides.springboot.repository;

import net.javaguides.springboot.model.Employee;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

Optional<Employee> findByEmail(String email);


}

And add the following code in EmployeeRepositoryTest Class

// JUnit test for get employee by email operation

@DisplayName("JUnit test for get employee by email operation")

@Test

public void givenEmployeeEmail_whenFindByEmail_thenReturnEmployeeObject(){

// given - precondition or setup

Employee employee = Employee.builder()

.firstName("Ramesh")

.lastName("Fadatare")

.email("ramesh@gmail,com")

.build();

employeeRepository.save(employee);

// when - action or the behaviour that we are going test

Employee employeeDB = employeeRepository.findByEmail(employee.getEmail()).get();

// then - verify the output

assertThat(employeeDB).isNotNull();

}
When we run will get the out put like this:
Unit test for update employee operation

And add the following code in EmployeeRepositoryTest Class

// JUnit test for update employee operation

@DisplayName("JUnit test for update employee operation")

@Test

public void givenEmployeeObject_whenUpdateEmployee_thenReturnUpdatedEmployee(){

// given - precondition or setup

Employee employee = Employee.builder()

.firstName("Ramesh")

.lastName("Fadatare")

.email("ramesh@gmail,com")

.build();

employeeRepository.save(employee);

// when - action or the behaviour that we are going test

Employee savedEmployee = employeeRepository.findById(employee.getId()).get();

savedEmployee.setEmail("[email protected]");

savedEmployee.setFirstName("Ram");

Employee updatedEmployee = employeeRepository.save(savedEmployee);

// then - verify the output

assertThat(updatedEmployee.getEmail()).isEqualTo("[email protected]");

assertThat(updatedEmployee.getFirstName()).isEqualTo("Ram");

If We want to debug the code we can do it like this:

First we have to make the break points like this:

And then run as Debug Junit Test .


And we can check each and every debug point by checking here:

We can check the above for every debug point.

And When we run the test we will get the output like this:

You might also like