Test/unit tests vet controller#184
Conversation
- Added unit tests for VetResponse DTO mapping (including @mapsid handling) - Added validation tests for VetRequest using Bean Validation - Implemented controller tests for VetController using MockMvc - Covered endpoints: POST /api/vets, GET /api/vets, GET /api/vets/{id} - Included tests for success and error scenarios (400, 403, 404) - Integrated GlobalExceptionHandler with custom exceptions - Ensured consistent exception handling and API response behavior
- Added unit tests for VetResponse DTO mapping (including @mapsid handling) - Added validation tests for VetRequest using Bean Validation - Implemented controller tests for VetController using MockMvc - Covered endpoints: POST /api/vets, GET /api/vets, GET /api/vets/{id} - Included tests for success and error scenarios (400, 403, 404) - Integrated GlobalExceptionHandler with custom exceptions - Ensured consistent exception handling and API response behavior
📝 WalkthroughWalkthroughThe changes add a new Jackson databind dependency to the Maven configuration and introduce a comprehensive Spring MVC test class for the VetController, covering creation, retrieval, authorization, validation, and error-handling scenarios across multiple HTTP endpoints. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
src/test/java/org/example/vet1177/controller/VetControllerTest.java (3)
99-111: Strengthen negative-path assertions with error payload checks.These tests currently validate only status codes. Adding JSON assertions (e.g.,
$.status,$.message) would better protectGlobalExceptionHandlercontract regressions.Also applies to: 164-172
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/controller/VetControllerTest.java` around lines 99 - 111, The tests (e.g., shouldReturn400_whenInvalidRequest and the other negative-path test around lines 164-172) only assert HTTP 400 and should be strengthened to also assert the error response JSON produced by GlobalExceptionHandler: verify fields like $.status (equals 400), $.message (contains validation error or specific message), and any $.errors array or $.timestamp expected by the contract. Update the mockMvc assertions to include jsonPath checks (e.g., .andExpect(jsonPath("$.status").value(400)) and .andExpect(jsonPath("$.message").exists() / containsString(...))) so the test verifies both status and error payload structure/contents.
34-34: Align ObjectMapper with other test classes for consistency.Other controller tests in the codebase (CommentControllerTest, MedicalRecordControllerTest) inject ObjectMapper via
@Autowired. Use the same pattern here for consistency and to ensure any custom Jackson configuration is automatically picked up if added in the future.♻️ Proposed change
- private final ObjectMapper objectMapper = new ObjectMapper(); + `@Autowired` + private ObjectMapper objectMapper;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/controller/VetControllerTest.java` at line 34, In VetControllerTest replace the manually constructed ObjectMapper field with the same injected pattern used in other tests: remove "new ObjectMapper()" and make the field an `@Autowired` ObjectMapper so the test class (VetControllerTest) receives the shared Jackson configuration; locate the private final ObjectMapper objectMapper declaration and change it to a non-final, `@Autowired` ObjectMapper field to match CommentControllerTest and MedicalRecordControllerTest.
68-69: Usenullable(User.class)instead ofany(User.class)for null-principal matchers.Since
@AutoConfigureMockMvc(addFilters = false)disables security filters, the@AuthenticationPrincipal User userparameter will benullat runtime. In Mockito 5.20.x,any(Class<T>)does not matchnull; usenullable(User.class)to explicitly match the expected null argument.♻️ Proposed change
- doNothing().when(adminPolicy).requireAdmin(any(User.class)); + doNothing().when(adminPolicy).requireAdmin(nullable(User.class)); @@ - doThrow(new ForbiddenException("Forbidden")) - .when(adminPolicy) - .requireAdmin(any()); + doThrow(new ForbiddenException("Forbidden")) + .when(adminPolicy) + .requireAdmin(nullable(User.class));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/controller/VetControllerTest.java` around lines 68 - 69, The test uses doNothing().when(adminPolicy).requireAdmin(any(User.class)) which fails to match the actual null `@AuthenticationPrincipal` at runtime; replace any(User.class) with nullable(User.class) in the requireAdmin stubbing so Mockito will match a null principal (i.e., update the doNothing().when(adminPolicy).requireAdmin(...) call to use nullable(User.class)); no other behavioral changes to vetService.createVet(...) are required.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/test/java/org/example/vet1177/controller/VetControllerTest.java`:
- Around line 99-111: The tests (e.g., shouldReturn400_whenInvalidRequest and
the other negative-path test around lines 164-172) only assert HTTP 400 and
should be strengthened to also assert the error response JSON produced by
GlobalExceptionHandler: verify fields like $.status (equals 400), $.message
(contains validation error or specific message), and any $.errors array or
$.timestamp expected by the contract. Update the mockMvc assertions to include
jsonPath checks (e.g., .andExpect(jsonPath("$.status").value(400)) and
.andExpect(jsonPath("$.message").exists() / containsString(...))) so the test
verifies both status and error payload structure/contents.
- Line 34: In VetControllerTest replace the manually constructed ObjectMapper
field with the same injected pattern used in other tests: remove "new
ObjectMapper()" and make the field an `@Autowired` ObjectMapper so the test class
(VetControllerTest) receives the shared Jackson configuration; locate the
private final ObjectMapper objectMapper declaration and change it to a
non-final, `@Autowired` ObjectMapper field to match CommentControllerTest and
MedicalRecordControllerTest.
- Around line 68-69: The test uses
doNothing().when(adminPolicy).requireAdmin(any(User.class)) which fails to match
the actual null `@AuthenticationPrincipal` at runtime; replace any(User.class)
with nullable(User.class) in the requireAdmin stubbing so Mockito will match a
null principal (i.e., update the doNothing().when(adminPolicy).requireAdmin(...)
call to use nullable(User.class)); no other behavioral changes to
vetService.createVet(...) are required.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: fb04511e-b17a-460d-8bc3-07682d4a43c1
📒 Files selected for processing (2)
pom.xmlsrc/test/java/org/example/vet1177/controller/VetControllerTest.java
Summary by CodeRabbit
Tests
Chores