-
Notifications
You must be signed in to change notification settings - Fork 0
Add Integration Test cases for Home Controller and Implement GlobalExceptionHandler with Custom CategoryNotFoundException #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@dileep-hegde I've opened a new pull request, #5, to work on those changes. Once the pull request is ready, I'll request review from you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces comprehensive unit tests for the HomeController while also implementing improved error handling through custom exceptions and refactoring REST API routes to follow RESTful conventions more closely.
- Added
HomeControllerTestswith 6 test methods covering various scenarios including successful responses, empty results, invalid IDs, missing IDs, and type mismatches - Implemented custom exception handling with
CategoryNotFoundException,GlobalExceptionHandler, andErrorResponseto provide structured error responses - Refactored REST endpoints to be more RESTful:
/categories/{categoryId}/topicsinstead of/topics/{categoryId}, and/topics/{slug}/quizinstead of/{slug}/quiz
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| HomeControllerTests.java | New test class with comprehensive test coverage for category and topic endpoints |
| HomeService.java | Updated to throw CategoryNotFoundException instead of generic RuntimeException |
| GlobalExceptionHandler.java | New global exception handler to catch CategoryNotFoundException and return appropriate HTTP status |
| ErrorResponse.java | New error response class to standardize error message structure |
| CategoryNotFoundException.java | New custom exception for category-related errors |
| QuizController.java | Refactored request mapping from /{slug}/quiz to /topics/{slug}/quiz for better REST semantics |
| HomeController.java | Refactored endpoint from /topics/{categoryId} to /categories/{categoryId}/topics for REST conventions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/test/java/com/javaProject/AptitudeApp/controller/HomeControllerTests.java
Show resolved
Hide resolved
src/main/java/com/javaProject/AptitudeApp/exception/ErrorResponse.java
Outdated
Show resolved
Hide resolved
src/test/java/com/javaProject/AptitudeApp/controller/HomeControllerTests.java
Outdated
Show resolved
Hide resolved
…ler with global exception handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/main/java/com/javaProject/AptitudeApp/exception/GlobalExceptionHandler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/javaProject/AptitudeApp/exception/ErrorResponse.java
Outdated
Show resolved
Hide resolved
| @Test | ||
| public void testGetTopicsByCategoryId_MissingId() throws Exception { | ||
|
|
||
| mockMvc.perform(get("/categories/{categoryId}/topics", "")) | ||
| .andExpect(status().isNotFound()); | ||
| } |
Copilot
AI
Dec 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test for missing ID (empty string path variable) expects a 404 Not Found status, but this scenario might actually result in a 405 Method Not Allowed or match a different route entirely. When an empty string is provided as the path variable, the URL becomes "/categories//topics" which may not match the expected endpoint pattern. Consider verifying the actual behavior or adjusting the test assertion accordingly.
src/test/java/com/javaProject/AptitudeApp/controller/HomeControllerTests.java
Outdated
Show resolved
Hide resolved
…er method name; improve unit tests for category ID handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/test/java/com/javaProject/AptitudeApp/controller/HomeControllerTests.java
Outdated
Show resolved
Hide resolved
… HomeService implementation
…estions; update resource retrieval mapping
Closes #7
This pull request introduces improved error handling for category-related endpoints, refactors some REST API routes for clarity and consistency, and adds comprehensive tests for the
HomeController. The main focus is on providing more informative error responses and ensuring the API adheres to RESTful conventions.Error handling improvements:
CategoryNotFoundExceptionand a global exception handler (GlobalExceptionHandler) to return structured error responses with appropriate HTTP status codes when a category is not found. Also introduced anErrorResponseclass to standardize error messages.HomeServiceto throwCategoryNotFoundExceptioninstead of a genericRuntimeExceptionwhen a category is not found.REST API route improvements:
/topics/{categoryId}to/categories/{categoryId}/topicsinHomeControllerfor better RESTful design.QuizControllerroutes to use/topics/{slug}/quizinstead of/{slug}/quiz, and changed the@GetMappingpath accordingly.Testing enhancements:
HomeControllerTestswith tests for all category and topic endpoints, including cases for empty results, invalid IDs, missing IDs, and type mismatches, ensuring robust coverage of both success and error scenarios.