Reactive Spring Boot with R2DBC with TDD using PostGreSql

git repo:

This article is about my hands on experience top develop a reactive sprint boot application using Test driven approach. These days TDD is a good idea to quickly show the concept move from an idea to a proved concept via CI/CD quickly.

This article assumes that you have good knowledge of how spring boot works and you have a initial setup of application.

Some basic setup should have Spring boot 2.3.1 or higher , spring boot reactive Web , Java 11, lombok, contract verifier, Spring data R2dbc, postgresql driver

It will look something like this



now open the code in IDE of your choice and make sure you have all the imports done properly.

Now lets start coding using TDD. My application is called EmployeeDataApiApplication so you will see it created EmployeeDataApiApplicationTests. Delete this Tests class

Create a class Test class in Test package under com.reactive.employeedataapi package

write a test case using @Test annotation. You can declare Employee and it will give error as Employee class doesnt exists. This will force us to create an Employee class in the main application.

Employee class can be created like this using Lombok

public class Employee {

private Integer id;
private String firstName;
private String LastName;


@Id is from;

Now Lets move back to our testcase and complete the tests

public class EmployeeTest {
public void test_createEmployeeBean(){

Employee employee=new Employee(1001,"Jack", "Smith");

This is very simple test case and should work.

Lets now try to create Test cases to Test our Repositories, so for that lets create test class under a new repository package called EmployeeRepositoryTests and try to create a persist code something like this. You can see that since we do not have the employeeRepository in our code it will give an error and that indicates we need to create a repsitory.

create and EmployeeRepository in main application under the package repository

public interface EmployeeRepository extends ReactiveCrudRepository<EmployeeRepository, Integer> {

now since we are working on R2DBC and postGreSQL, I am assuming you have created a postGreSQL server DB and have a table called Employee.

The connection in application.yml file will looks like this

url: r2dbc:postgres://localhost/emp
username: postgres
password: *******

Now lets go back to EmployeeREpositoryTests class and add the annotations @ExtendWith(SpringExtension.class) and @DataR2dbcTest .We are using @ExtendWith( SpringExtension.class ) to support testing in Junit 5. In Junit 4, we need to use @RunWith(SpringRunner.class).

public class EmployeeRepositoryTests {

EmployeeRepository employeeRepository;

public void tests_persisEmployee(){

Flux<Employee> actual = this.employeeRepository.deleteAll()
.thenMany( Employee(null, "Tina", "Fey")))



This concludes the Repository test API. The next Article will include the Resource layer i.e. Controller testing using the same project.



SE-Manager #Hiring #Mentorship #DEI #SoftwareDelivery #KPI #OKR #java-J2EE #Nodejs #GraphQl #Aws

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
abhinav singhal

SE-Manager #Hiring #Mentorship #DEI #SoftwareDelivery #KPI #OKR #java-J2EE #Nodejs #GraphQl #Aws