Reactive Spring Boot with R2DBC with TDD using PostGreSql

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {

@Id
private Integer id;
private String firstName;
private String LastName;

}
public class EmployeeTest {
@Test
public void test_createEmployeeBean(){

Employee employee=new Employee(1001,"Jack", "Smith");
Assertions.assertEquals(employee.getId(),1001);
Assertions.assertEquals(employee.getFirstName(),"Jack");
Assertions.assertEquals(employee.getLastName(),"Smith");
}
}
public interface EmployeeRepository extends ReactiveCrudRepository<EmployeeRepository, Integer> {
}


spring:
r2dbc:
url: r2dbc:postgres://localhost/emp
username: postgres
password: *******
@ExtendWith(SpringExtension.class)
@DataR2dbcTest
public class EmployeeRepositoryTests {

@Autowired
EmployeeRepository employeeRepository;

@Test
public void tests_persisEmployee(){

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

StepVerifier.create(actual)
.expectNextMatches(result->result.getFirstName().equalsIgnoreCase("tina"))
.verifyComplete();
}

}

--

--

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

abhinav singhal

9 Followers

Technology enthusiast exploring smart ways of building enterprise software(s) , a marathon run addict and a water-colour artist