Skip to content

Commit 04193d2

Browse files
committed
test cases added
1 parent f5e8cea commit 04193d2

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

reactive-micro-service/student-service/src/main/java/com/prs/services/student/service/StudentServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.stereotype.Service;
99

10-
import com.prs.services.exceptionHandler.DataException;
10+
import com.prs.services.exceptionHandler.NotFoundException;
1111
import com.prs.services.student.client.CollegeReactiveClient;
1212
import com.prs.services.student.client.DepartmentReactiveClient;
1313
import com.prs.services.student.entity.StudentEntity;
@@ -42,7 +42,7 @@ public Mono<Student> update(Long id, StudentEntity entity) {
4242
return findById(id).flatMap(s-> {
4343
entity.setId(id);
4444
return save(entity);
45-
}).switchIfEmpty(Mono.error(new DataException("Student not fount for id : "+id)));
45+
}).switchIfEmpty(Mono.error(new NotFoundException("Student not fount for id : "+id)));
4646
}
4747

4848
@Override

reactive-micro-service/student-service/src/test/java/com/prs/services/student/controller/StudentControllerIntegrationTest.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.prs.services.student.controller;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
45

56
import java.util.List;
67

8+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
9+
import org.junit.jupiter.api.Order;
710
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.TestMethodOrder;
812
import org.springframework.beans.factory.annotation.Autowired;
913
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
1014
import org.springframework.boot.test.context.SpringBootTest;
@@ -18,6 +22,7 @@
1822
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
1923
@ActiveProfiles("test")
2024
@AutoConfigureWebTestClient
25+
@TestMethodOrder(OrderAnnotation.class)
2126
public class StudentControllerIntegrationTest {
2227

2328
@Autowired
@@ -26,6 +31,8 @@ public class StudentControllerIntegrationTest {
2631
@Autowired
2732
private StudentRepository studentRepository;
2833

34+
static Long testId;
35+
2936
// @BeforeEach
3037
void setUp() {
3138
var students = List.of(new StudentEntity(null,"Prasath",31,1l,1l),
@@ -39,6 +46,7 @@ void setUp() {
3946
}
4047

4148
@Test
49+
@Order(1)
4250
void getAllStudents() {
4351
webTestClient
4452
.get()
@@ -56,32 +64,56 @@ void getAllStudents() {
5664
}
5765

5866
@Test
67+
@Order(2)
68+
void addStudent() {
69+
var student = new StudentEntity(null,"Prasath",31,1l,1l);
70+
webTestClient
71+
.post()
72+
.uri("/add")
73+
.bodyValue(student)
74+
.exchange()
75+
.expectStatus()
76+
.isCreated()
77+
.expectBody(Student.class)
78+
.consumeWith(result -> {
79+
var s = result.getResponseBody();
80+
assert s != null;
81+
assertNotEquals(null, s);
82+
assertEquals("Prasath", s.getName());
83+
testId = s.getId();
84+
System.out.println("testId received : "+testId);
85+
assertNotEquals(null, testId);
86+
});
87+
}
88+
89+
@Test
90+
@Order(3)
5991
void getStudentById() {
60-
Long id = 1l;
92+
System.out.println("testId in getStudentById: "+testId);
6193
webTestClient
6294
.get()
63-
.uri("/get-by/{id}", id)
95+
.uri("/get-by/{id}", testId)
6496
.exchange()
6597
.expectStatus()
6698
.is2xxSuccessful()
6799
.expectBody(Student.class)
68100
.consumeWith(result -> {
69101
var student = result.getResponseBody();
70102
assert student != null;
71-
assertEquals("aa", student.getName());
103+
assertEquals("Prasath", student.getName());
72104
});
73105
//.expectBody() .jsonPath("$.name").isEqualTo("Prasath");
74106

75107
}
76108

77109
@Test
110+
@Order(4)
78111
void updateStudent() {
79-
var id = 2l;
80-
var updatedStudent = new StudentEntity(2l,"Nishanthi M",30,2l,2l);
112+
var updatedStudent = new StudentEntity(testId,"Nishanthi M",30,2l,2l);
81113

82114
webTestClient
83115
.put()
84-
.uri("/update/{id}", id)
116+
.uri("/update/{id}", testId)
85117
.bodyValue(updatedStudent)
86118
.exchange()
87119
.expectStatus()
@@ -95,9 +127,10 @@ void updateStudent() {
95127
}
96128

97129
@Test
130+
@Order(5)
98131
void updateStudent_notFound() {
99132
var id = 5l;
100-
var updatedStudent = new StudentEntity(2l,"Nishanthi M",30,2l,2l);
133+
var updatedStudent = new StudentEntity(testId,"Nishanthi M",30,2l,2l);
101134

102135
webTestClient
103136
.put()
@@ -109,12 +142,11 @@ void updateStudent_notFound() {
109142
}
110143

111144
@Test
145+
@Order(6)
112146
void deleteStudentById() {
113-
var id = 2l;
114-
115147
webTestClient
116148
.delete()
117-
.uri("/delete/{id}", id)
149+
.uri("/delete/{id}", testId)
118150
.exchange()
119151
.expectStatus()
120152
.isNoContent();

reactive-micro-service/student-service/src/test/java/com/prs/services/student/cucumberConfig/CucumberSpringConfiguration.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
package com.prs.services.student.cucumberConfig;
33

44
import org.springframework.boot.test.context.SpringBootTest;
5-
import org.springframework.test.context.ContextConfiguration;
6-
7-
import com.prs.services.StudentApplication;
85

96
import io.cucumber.spring.CucumberContextConfiguration;
107

118
@CucumberContextConfiguration
129
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
13-
//@SpringBootTest(classes = StudentApplication.class)
1410
public class CucumberSpringConfiguration {
1511
}

reactive-micro-service/student-service/src/test/java/com/prs/services/student/cucumberConfig/StudentCucumberSteps.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
75
import org.springframework.beans.factory.annotation.Autowired;
86
import org.springframework.test.web.reactive.server.WebTestClient;
97

@@ -15,8 +13,6 @@
1513
import io.cucumber.java.en.When;
1614

1715
public class StudentCucumberSteps {
18-
19-
private final Logger log = LoggerFactory.getLogger(StudentCucumberSteps.class);
2016
StudentEntity studentTest = new StudentEntity(null, "testCucumber", 25, 2l, 2l);
2117
@Autowired
2218
private WebTestClient webTestClient;

0 commit comments

Comments
 (0)