test: add MVC tests for MedicalRecordController#162
Conversation
- Introduced `MedicalRecordControllerTest` with extensive coverage for all endpoints. - Verified request/response handling for create, update, assign vet, close, and query operations. - Ensured proper exception handling for validation errors, forbidden access, and not found scenarios.Closes #101
📝 WalkthroughWalkthroughA new Spring MVC slice test class ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/test/java/org/example/vet1177/controller/MedicalRecordControllerTest.java (2)
229-236: Tighten owner-scope assertion by binding to the authenticated owner ID.Using
any(UUID.class)here makes the test pass even if the controller forwards the wrong owner ID. Prefereq(ownerUser.getId())(and optionally verify invocation) to lock the access rule behavior.🤖 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/MedicalRecordControllerTest.java` around lines 229 - 236, The test getMyRecords_asOwner_shouldReturn200WithList currently stubs medicalRecordService.getByOwner(any(UUID.class)) which allows the controller to pass any ID; change the stub to use eq(ownerUser.getId()) so the service is only invoked for the authenticated owner's UUID (i.e., when(medicalRecordService.getByOwner(eq(ownerUser.getId()))). Optionally add a verify(medicalRecordService).getByOwner(eq(ownerUser.getId())) after the request to assert the controller forwarded the correct owner ID.
111-125: Strengthen DTO contract checks for success responses.Current assertions validate selected fields only. To match the payload-contract objective, add assertions for the remaining expected
MedicalRecordResponsefields (existence/value/nullability) in representative happy-path tests.🤖 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/MedicalRecordControllerTest.java` around lines 111 - 125, In create_shouldReturn200WithMedicalRecordResponse add JSON assertions to cover the full MedicalRecordResponse contract: assert the presence and/or expected values for remaining fields from the returned record (e.g., jsonPath("$.id").exists(), jsonPath("$.description").value("Hostar mycket"), jsonPath("$.createdAt").isNotEmpty(), jsonPath("$.updatedAt").value(org.hamcrest.Matchers.nullValue() or expected value), jsonPath("$.clinicId").value(clinicId), jsonPath("$.vetId").value(vetUser.getId()), jsonPath("$.petId").value(petId)) so the test verifies all DTO fields returned by the controller for the happy path; use the existing record/test fixtures to pick expected values.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@src/test/java/org/example/vet1177/controller/MedicalRecordControllerTest.java`:
- Around line 519-527: Update the test close_whenAlreadyClosed_shouldReturn409
to assert a 409 Conflict explicitly: replace the generic
.andExpect(status().is4xxClientError()) assertion on the mockMvc.perform PUT to
/api/medical-records/{id}/close with .andExpect(status().isConflict()), keeping
the same setup of when(medicalRecordService.close(eq(recordId),
any(User.class))).thenThrow(new BusinessRuleException(...)) and the
authenticatedAs(vetUser) request so the test fails if the controller returns any
status other than 409.
---
Nitpick comments:
In
`@src/test/java/org/example/vet1177/controller/MedicalRecordControllerTest.java`:
- Around line 229-236: The test getMyRecords_asOwner_shouldReturn200WithList
currently stubs medicalRecordService.getByOwner(any(UUID.class)) which allows
the controller to pass any ID; change the stub to use eq(ownerUser.getId()) so
the service is only invoked for the authenticated owner's UUID (i.e.,
when(medicalRecordService.getByOwner(eq(ownerUser.getId()))). Optionally add a
verify(medicalRecordService).getByOwner(eq(ownerUser.getId())) after the request
to assert the controller forwarded the correct owner ID.
- Around line 111-125: In create_shouldReturn200WithMedicalRecordResponse add
JSON assertions to cover the full MedicalRecordResponse contract: assert the
presence and/or expected values for remaining fields from the returned record
(e.g., jsonPath("$.id").exists(), jsonPath("$.description").value("Hostar
mycket"), jsonPath("$.createdAt").isNotEmpty(),
jsonPath("$.updatedAt").value(org.hamcrest.Matchers.nullValue() or expected
value), jsonPath("$.clinicId").value(clinicId),
jsonPath("$.vetId").value(vetUser.getId()), jsonPath("$.petId").value(petId)) so
the test verifies all DTO fields returned by the controller for the happy path;
use the existing record/test fixtures to pick expected values.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4f5abbf5-6b43-4579-991d-3848a655d393
📒 Files selected for processing (1)
src/test/java/org/example/vet1177/controller/MedicalRecordControllerTest.java
| void close_whenAlreadyClosed_shouldReturn409() throws Exception { | ||
| when(medicalRecordService.getById(recordId)).thenReturn(record); | ||
| when(medicalRecordService.close(eq(recordId), any(User.class))) | ||
| .thenThrow(new BusinessRuleException("Ärendet är redan stängt")); | ||
|
|
||
| mockMvc.perform(put("/api/medical-records/{id}/close", recordId) | ||
| .with(authenticatedAs(vetUser))) | ||
| .andExpect(status().is4xxClientError()); | ||
| } |
There was a problem hiding this comment.
Make the 409 expectation explicit in the close conflict test.
close_whenAlreadyClosed_shouldReturn409 currently accepts any 4xx on Line 526, which can hide a wrong mapping (e.g., 400/403). Assert 409 Conflict directly.
Suggested change
- .andExpect(status().is4xxClientError());
+ .andExpect(status().isConflict());🤖 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/MedicalRecordControllerTest.java`
around lines 519 - 527, Update the test close_whenAlreadyClosed_shouldReturn409
to assert a 409 Conflict explicitly: replace the generic
.andExpect(status().is4xxClientError()) assertion on the mockMvc.perform PUT to
/api/medical-records/{id}/close with .andExpect(status().isConflict()), keeping
the same setup of when(medicalRecordService.close(eq(recordId),
any(User.class))).thenThrow(new BusinessRuleException(...)) and the
authenticatedAs(vetUser) request so the test fails if the controller returns any
status other than 409.
MedicalRecordControllerTestwith extensive coverage for all endpoints.Summary by CodeRabbit