Skip to content

Conversation

@dileep-hegde
Copy link
Member

@dileep-hegde dileep-hegde commented Dec 18, 2025

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:

  • Added a custom exception CategoryNotFoundException and a global exception handler (GlobalExceptionHandler) to return structured error responses with appropriate HTTP status codes when a category is not found. Also introduced an ErrorResponse class to standardize error messages.
  • Updated HomeService to throw CategoryNotFoundException instead of a generic RuntimeException when a category is not found.

REST API route improvements:

  • Changed the endpoint for fetching topics by category from /topics/{categoryId} to /categories/{categoryId}/topics in HomeController for better RESTful design.
  • Updated QuizController routes to use /topics/{slug}/quiz instead of /{slug}/quiz, and changed the @GetMapping path accordingly.

Testing enhancements:

  • Added a new test class HomeControllerTests with 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.

@dileep-hegde
Copy link
Member Author

@copilot

Copy link

Copilot AI commented Dec 18, 2025

@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.

Copy link

Copilot AI left a 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 HomeControllerTests with 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, and ErrorResponse to provide structured error responses
  • Refactored REST endpoints to be more RESTful: /categories/{categoryId}/topics instead of /topics/{categoryId}, and /topics/{slug}/quiz instead 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.

Copy link

Copilot AI left a 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.

Comment on lines +105 to +110
@Test
public void testGetTopicsByCategoryId_MissingId() throws Exception {

mockMvc.perform(get("/categories/{categoryId}/topics", ""))
.andExpect(status().isNotFound());
}
Copy link

Copilot AI Dec 18, 2025

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.

Copilot uses AI. Check for mistakes.
…er method name; improve unit tests for category ID handling
Copy link

Copilot AI left a 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.

@ProDevCollab ProDevCollab deleted a comment from Copilot AI Dec 18, 2025
@ProDevCollab ProDevCollab deleted a comment from Copilot AI Dec 18, 2025
@ProDevCollab ProDevCollab deleted a comment from Copilot AI Dec 18, 2025
@ProDevCollab ProDevCollab deleted a comment from Copilot AI Dec 18, 2025
@ProDevCollab ProDevCollab deleted a comment from Copilot AI Dec 18, 2025
@ProDevCollab ProDevCollab deleted a comment from Copilot AI Dec 18, 2025
@dileep-hegde dileep-hegde changed the title Add unit test cases for HomeController Add HomeController Test case and Implement GlobalExceptionHandler with Custom CategoryNotFoundException Dec 18, 2025
@dileep-hegde dileep-hegde changed the title Add HomeController Test case and Implement GlobalExceptionHandler with Custom CategoryNotFoundException Add HomeController Test cases and Implement GlobalExceptionHandler with Custom CategoryNotFoundException Dec 18, 2025
@dileep-hegde dileep-hegde changed the title Add HomeController Test cases and Implement GlobalExceptionHandler with Custom CategoryNotFoundException Add Integration Test cases for Home Controller and Implement GlobalExceptionHandler with Custom CategoryNotFoundException Dec 19, 2025
@dileep-hegde dileep-hegde merged commit bc9111d into main Dec 19, 2025
@dileep-hegde dileep-hegde deleted the add-unit-test-cases branch January 16, 2026 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Integration Testing for HomeController

3 participants