Skip to content

Commit bab50bf

Browse files
Ashish MishraAshish Mishra
authored andcommitted
adding testGetUserByIdApi() & testGetAllUsersApi()
1 parent b07e8fd commit bab50bf

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ docker-compose --profile entireApp down
8484
```yaml
8585
mysql-service:
8686
image: mysql:latest
87-
restart: always
8887
environment:
8988
MYSQL_DATABASE: userManagementDb
9089
MYSQL_ROOT_PASSWORD: ashish@123
@@ -111,7 +110,6 @@ docker-compose --profile entireApp down
111110
```yaml
112111
mysql-service-onlyDb:
113112
image: mysql:latest
114-
restart: always
115113
environment:
116114
MYSQL_DATABASE: userManagementDb
117115
MYSQL_ROOT_PASSWORD: ashish@123
@@ -139,7 +137,6 @@ docker-compose --profile entireApp down
139137
```yaml
140138
adminer-service:
141139
image: adminer
142-
restart: always
143140
ports:
144141
- "8081:8080"
145142
profiles:

docker-compose.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ services:
22

33
mysql-service:
44
image: mysql:latest
5-
restart: always
65
environment:
76
MYSQL_DATABASE: userManagementDb
87
MYSQL_ROOT_PASSWORD: ashish@123
@@ -18,7 +17,6 @@ services:
1817

1918
mysql-service-onlyDb:
2019
image: mysql:latest
21-
restart: always
2220
environment:
2321
MYSQL_DATABASE: userManagementDb
2422
MYSQL_ROOT_PASSWORD: ashish@123
@@ -34,7 +32,6 @@ services:
3432

3533
adminer-service:
3634
image: adminer
37-
restart: always
3835
ports:
3936
- "8081:8080"
4037
profiles:

src/main/resources/dockerWithDb/docker-compose.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ services:
44

55
mysql-service:
66
image: mysql:latest
7-
restart: always
87
environment:
98
MYSQL_DATABASE: userManagementDb
109
MYSQL_ROOT_PASSWORD: ashish@123
@@ -18,6 +17,5 @@ services:
1817

1918
adminer-service:
2019
image: adminer
21-
restart: always
2220
ports:
2321
- "8081:8080"

src/test/java/com/beneite/SpringWithDocker/controllerTest/UserControllerTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import org.springframework.http.HttpStatus;
1313
import org.springframework.http.ResponseEntity;
1414

15+
import java.util.List;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
1518
import static org.junit.jupiter.api.Assertions.assertEquals;
1619
import static org.junit.jupiter.api.Assertions.assertNotNull;
1720
import static org.mockito.ArgumentMatchers.any;
@@ -39,10 +42,69 @@ public void testCreateUserApi(){
3942
// Assert: Validate the response
4043
assertNotNull(createUserResponse, "Response should not be null");
4144
assertEquals(HttpStatus.CREATED, createUserResponse.getStatusCode());
45+
46+
// asserting the response body data
47+
assertEquals(createUserResponse.getBody().getId(), buildCreateUserDto().getId());
48+
assertEquals(createUserResponse.getBody().getEmail(), buildCreateUserDto().getEmail());
49+
assertEquals(createUserResponse.getBody().getFirstName(), buildCreateUserDto().getFirstName());
50+
assertEquals(createUserResponse.getBody().getLastName(), buildCreateUserDto().getLastName());
51+
52+
}
53+
54+
@Test
55+
public void testGetUserByIdApi() {
56+
// Given: Mock Service Layer
57+
when(userServiceImpl.getUserByIdImplementation(buildGetUserByIdDto().getId()))
58+
.thenReturn(buildCreateUserDto());
59+
60+
// Act: Call the controller method
61+
ResponseEntity<UserDto> getUserByIdResponse = userController.getUserByIdApi(buildGetUserByIdDto().getId());
62+
63+
// Assert: Validate the response
64+
assertNotNull(getUserByIdResponse, "Response should not be null");
65+
assertEquals(HttpStatus.OK, getUserByIdResponse.getStatusCode());
66+
67+
// asserting the response body data
68+
assertEquals(getUserByIdResponse.getBody().getId(), buildCreateUserDto().getId());
69+
assertEquals(getUserByIdResponse.getBody().getEmail(), buildCreateUserDto().getEmail());
70+
assertEquals(getUserByIdResponse.getBody().getFirstName(), buildCreateUserDto().getFirstName());
71+
assertEquals(getUserByIdResponse.getBody().getLastName(), buildCreateUserDto().getLastName());
72+
73+
}
74+
75+
@Test
76+
public void testGetAllUsersApi(){
77+
when(userServiceImpl.getAllUsersImplementation())
78+
.thenReturn(buildGetAllUsersDto());
79+
80+
// Act: Call the controller method
81+
ResponseEntity<List<UserDto>> getAllUsersResponse = userController.getAllUsersApi();
82+
83+
// Assert: Validate the response
84+
assertNotNull(getAllUsersResponse);
85+
assertEquals(HttpStatus.OK, getAllUsersResponse.getStatusCode());
86+
assertEquals(buildGetAllUsersDto().size(), getAllUsersResponse.getBody().size());
87+
assertNotNull(getAllUsersResponse.getBody());
88+
89+
// ✅ Compare Lists Using AssertJ (Recommended)
90+
assertThat(getAllUsersResponse.getBody()).usingRecursiveComparison().isEqualTo(buildGetAllUsersDto());
91+
4292
}
4393

4494
private UserDto buildCreateUserDto(){
4595
return new UserDto(1L, "Ashish.Mishra@yopmail.com", "Ashish", "Mishra");
4696
}
4797

98+
private UserDto buildGetUserByIdDto(){
99+
return new UserDto(10L, "Ashish.Mishra@yopmail.com", "Ashish", "Mishra");
100+
}
101+
102+
private List<UserDto> buildGetAllUsersDto(){
103+
return List.of(new UserDto(10L, "Ashish.Mishra@yopmail.com", "Ashish", "Mishra"),
104+
new UserDto(11L, "Manish.arthore@yopmail.com", "Manish", "arthore"),
105+
new UserDto(1L, "Alok.Jha@yopmail.com", "Alok", "Jha")
106+
);
107+
108+
}
109+
48110
}

0 commit comments

Comments
 (0)