Skip to content

Commit f979433

Browse files
committed
test: add unit tests for EmailNotFoundException and LoginException handlers
1 parent 363dcd0 commit f979433

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/main/java/fun/trackmoney/config/exception/RestExceptionHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class RestExceptionHandler {
1919

2020
@ExceptionHandler(PasswordNotValid.class)
21-
public ResponseEntity<ApiResponse<List<String>>> passwordNotValid(PasswordNotValid ex) {
21+
public ResponseEntity<ApiResponse<List<CustomFieldError>>> passwordNotValid(PasswordNotValid ex) {
2222
return ResponseEntity.badRequest().body(
2323
new ApiResponse<>(false, ex.getMessage(), null, ex.getErrors())
2424
);
@@ -45,15 +45,15 @@ public ResponseEntity<ApiResponse<List<CustomFieldError>>> handleValidationExcep
4545
}
4646

4747
@ExceptionHandler(EmailAlreadyExistsException.class)
48-
public ResponseEntity<ApiResponse<Object>> handleEmailAlreadyExists(EmailAlreadyExistsException ex) {
48+
public ResponseEntity<ApiResponse<List<CustomFieldError>>> handleEmailAlreadyExists(EmailAlreadyExistsException ex) {
4949
return ResponseEntity
5050
.status(HttpStatus.CONFLICT)
5151
.body(new ApiResponse<>(false, ex.getMessage(), null,
5252
List.of(new CustomFieldError("Email", ex.getMessage()))));
5353
}
5454

5555
@ExceptionHandler(EmailNotFoundException.class)
56-
public ResponseEntity<ApiResponse<List<String>>> emailNotFound(EmailNotFoundException ex) {
56+
public ResponseEntity<ApiResponse<List<CustomFieldError>>> emailNotFound(EmailNotFoundException ex) {
5757
return ResponseEntity.badRequest().body(
5858
new ApiResponse<>(false, ex.getMessage(), null, ex.getErrors())
5959
);

src/test/java/fun/trackmoney/config/exception/RestExceptionHandlerTest.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package fun.trackmoney.config.exception;
22

3+
import fun.trackmoney.auth.exception.LoginException;
34
import fun.trackmoney.user.exception.EmailAlreadyExistsException;
5+
import fun.trackmoney.user.exception.EmailNotFoundException;
46
import fun.trackmoney.user.exception.PasswordNotValid;
57
import fun.trackmoney.utils.CustomFieldError;
68
import fun.trackmoney.utils.response.ApiResponse;
@@ -11,7 +13,6 @@
1113
import org.springframework.validation.FieldError;
1214
import org.springframework.web.bind.MethodArgumentNotValidException;
1315
import java.util.List;
14-
1516
import static org.junit.jupiter.api.Assertions.*;
1617
import static org.mockito.Mockito.mock;
1718
import static org.mockito.Mockito.when;
@@ -29,10 +30,10 @@ void passwordNotValidShouldReturnBadRequestWithCorrectErrors() {
2930
new CustomFieldError("password", "must be at least 8 characters long")
3031
);
3132
PasswordNotValid exception = new PasswordNotValid(errors);
32-
ResponseEntity<ApiResponse<List<String>>> response = restExceptionHandler.passwordNotValid(exception);
33+
ResponseEntity<ApiResponse<List<CustomFieldError>>> response = restExceptionHandler.passwordNotValid(exception);
3334

3435
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
35-
ApiResponse<List<String>> apiResponse = response.getBody();
36+
ApiResponse<List<CustomFieldError>> apiResponse = response.getBody();
3637

3738
assertNotNull(apiResponse);
3839
assertFalse(apiResponse.isSuccess());
@@ -48,10 +49,10 @@ void passwordNotValidShouldReturnBadRequestWithCorrectErrors() {
4849
@Test
4950
void handleEmailAlreadyExistsShouldReturnConflictWithError() {
5051
EmailAlreadyExistsException exception = new EmailAlreadyExistsException("This email is already in use.");
51-
ResponseEntity<ApiResponse<Object>> response = restExceptionHandler.handleEmailAlreadyExists(exception);
52+
ResponseEntity<ApiResponse<List<CustomFieldError>>> response = restExceptionHandler.handleEmailAlreadyExists(exception);
5253

5354
assertEquals(HttpStatus.CONFLICT, response.getStatusCode());
54-
ApiResponse<Object> apiResponse = response.getBody();
55+
ApiResponse<List<CustomFieldError>> apiResponse = response.getBody();
5556
assertNotNull(apiResponse);
5657
assertFalse(apiResponse.isSuccess());
5758
assertEquals("This email is already in use.", apiResponse.getMessage());
@@ -98,4 +99,40 @@ void handleValidationExceptions_shouldReturnBadRequestWithDetailedFieldErrors()
9899
.anyMatch(error -> error.getField().equals("object") && error.getMessage().equals("Global error")),
99100
"Global validation error is missing or incorrect");
100101
}
102+
103+
@Test
104+
void handleEmailNotFound_shouldReturnBadRequestWithErrorMessage() {
105+
List<CustomFieldError> errors = List.of(
106+
new CustomFieldError("user", "Email not found!")
107+
);
108+
EmailNotFoundException exception = new EmailNotFoundException(errors);
109+
ResponseEntity<ApiResponse<List<CustomFieldError>>> response = restExceptionHandler.emailNotFound(exception);
110+
111+
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
112+
ApiResponse<List<CustomFieldError>> apiResponse = response.getBody();
113+
114+
assertNotNull(apiResponse);
115+
assertFalse(apiResponse.isSuccess());
116+
assertEquals("Email not found!", apiResponse.getMessage());
117+
assertNotNull(apiResponse.getErrors());
118+
assertEquals(errors.size(), apiResponse.getErrors().size());
119+
}
120+
121+
@Test
122+
void handleLoginException_shouldReturnBadRequestWithPasswordError() {
123+
List<CustomFieldError> errors = List.of(
124+
new CustomFieldError("Password", "Password is incorrect.")
125+
);
126+
LoginException exception = new LoginException(errors);
127+
ResponseEntity<ApiResponse<List<CustomFieldError>>> response = restExceptionHandler.loginException(exception);
128+
129+
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
130+
ApiResponse<List<CustomFieldError>> apiResponse = response.getBody();
131+
132+
assertNotNull(apiResponse);
133+
assertFalse(apiResponse.isSuccess());
134+
assertEquals("Password is incorrect.", apiResponse.getErrors().get(0).getMessage());
135+
assertNotNull(apiResponse.getErrors());
136+
assertEquals(errors.size(), apiResponse.getErrors().size());
137+
}
101138
}

0 commit comments

Comments
 (0)