Skip to content

Commit 31bb3d3

Browse files
committed
test: add unit test for RestExceptionHandler.goalsNotFound
1 parent f9258aa commit 31bb3d3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fun.trackmoney.account.exception.AccountNotFoundException;
44
import fun.trackmoney.auth.exception.LoginException;
55
import fun.trackmoney.category.exception.CategoryNotFoundException;
6+
import fun.trackmoney.goal.exception.GoalsNotFoundException;
67
import fun.trackmoney.transaction.exception.TransactionNotFoundException;
78
import fun.trackmoney.user.exception.EmailAlreadyExistsException;
89
import fun.trackmoney.user.exception.UserNotFoundException;
@@ -92,4 +93,12 @@ public ResponseEntity<ApiResponse<List<CustomFieldError>>> transactionNotFound(T
9293
.body(new ApiResponse<>(false, ex.getMessage(), null, ex.getErrors())
9394
);
9495
}
96+
97+
@ExceptionHandler(GoalsNotFoundException.class)
98+
public ResponseEntity<ApiResponse<List<CustomFieldError>>> goalsNotFound(GoalsNotFoundException ex) {
99+
return ResponseEntity
100+
.status(HttpStatus.NOT_FOUND)
101+
.body(new ApiResponse<>(false, ex.getMessage(), null, ex.getErrors())
102+
);
103+
}
95104
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fun.trackmoney.account.exception.AccountNotFoundException;
44
import fun.trackmoney.auth.exception.LoginException;
55
import fun.trackmoney.category.exception.CategoryNotFoundException;
6+
import fun.trackmoney.goal.exception.GoalsNotFoundException;
67
import fun.trackmoney.transaction.exception.TransactionNotFoundException;
78
import fun.trackmoney.user.exception.EmailAlreadyExistsException;
89
import fun.trackmoney.user.exception.UserNotFoundException;
@@ -193,4 +194,22 @@ void TransactionNotFound_shouldReturnNotFoundWithError() {
193194
assertEquals("Transaction not found.", error.getMessage());
194195
}
195196

197+
@Test
198+
void goalsNotFound_shouldReturnNotFoundWithError() {
199+
GoalsNotFoundException exception = new GoalsNotFoundException("Goals not found.");
200+
ResponseEntity<ApiResponse<List<CustomFieldError>>> response = restExceptionHandler.goalsNotFound(exception);
201+
202+
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
203+
ApiResponse<List<CustomFieldError>> apiResponse = response.getBody();
204+
assertNotNull(apiResponse);
205+
assertFalse(apiResponse.isSuccess());
206+
assertEquals("Goals not found.", apiResponse.getMessage());
207+
assertNull(apiResponse.getData());
208+
assertNotNull(apiResponse.getErrors());
209+
assertEquals(1, (apiResponse.getErrors()).size());
210+
CustomFieldError error = apiResponse.getErrors().get(0);
211+
assertEquals("Goal", error.getField());
212+
assertEquals("Goals not found.", error.getMessage());
213+
}
214+
196215
}

0 commit comments

Comments
 (0)