|
12 | 12 | import org.springframework.http.HttpStatus; |
13 | 13 | import org.springframework.http.ResponseEntity; |
14 | 14 |
|
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
15 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; |
16 | 19 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
17 | 20 | import static org.mockito.ArgumentMatchers.any; |
@@ -39,10 +42,69 @@ public void testCreateUserApi(){ |
39 | 42 | // Assert: Validate the response |
40 | 43 | assertNotNull(createUserResponse, "Response should not be null"); |
41 | 44 | 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 | + |
42 | 92 | } |
43 | 93 |
|
44 | 94 | private UserDto buildCreateUserDto(){ |
45 | 95 | return new UserDto(1L, "Ashish.Mishra@yopmail.com", "Ashish", "Mishra"); |
46 | 96 | } |
47 | 97 |
|
| 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 | + |
48 | 110 | } |
0 commit comments