test: add unit tests for MedicalRecord entity and response DTOs #139
test: add unit tests for MedicalRecord entity and response DTOs #139annikaholmqvist94 merged 3 commits intomainfrom
Conversation
- Created `MedicalRecordEntityTest` to provide comprehensive test coverage for `MedicalRecord`. - Included tests for getters, setters, status logic, attachments handling, and lifecycle methods (`onCreate`, `onUpdate`).
- Introduced `MedicalRecordSummaryResponseTest` to validate mapping logic for all fields. - Added tests for both happy paths (e.g., correct mapping, null handling) and sad paths (e.g., `NullPointerException` for required fields). Closes #99
📝 WalkthroughWalkthroughThis PR introduces three comprehensive JUnit 5 test classes for the medical record domain: Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/test/java/org/example/vet1177/entities/MedicalRecordEntityTest.java (1)
220-238: StrengthenonUpdateassertion and remove overlapping test intent.
onUpdate_shouldRefreshUpdatedAtWithoutModifyingCreatedAtcurrently verifies non-null + stablecreatedAt, but not thatupdatedAtactually moves forward. Also, Line 231-238 overlaps this behavior.♻️ Proposed consolidation
`@Test` void onUpdate_shouldRefreshUpdatedAtWithoutModifyingCreatedAt() { record.onCreate(); Instant createdAt = record.getCreatedAt(); + Instant previousUpdatedAt = record.getUpdatedAt(); record.onUpdate(); - assertThat(record.getUpdatedAt()).isNotNull(); assertThat(record.getCreatedAt()).isEqualTo(createdAt); + assertThat(record.getUpdatedAt()).isNotNull(); + assertThat(record.getUpdatedAt()).isAfterOrEqualTo(previousUpdatedAt); } - -@Test -void onUpdate_shouldNotModifyCreatedAt() { - record.onCreate(); - Instant originalCreatedAt = record.getCreatedAt(); - - record.onUpdate(); - - assertThat(record.getCreatedAt()).isEqualTo(originalCreatedAt); -}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/entities/MedicalRecordEntityTest.java` around lines 220 - 238, The two tests overlap: consolidate into a single test (e.g., onUpdate_shouldRefreshUpdatedAtWithoutModifyingCreatedAt) that calls record.onCreate(), captures Instant originalCreatedAt = record.getCreatedAt(), waits or ensures time advances if needed, calls record.onUpdate(), then assertThat(record.getUpdatedAt()).isAfter(originalCreatedAt) and assertThat(record.getCreatedAt()).isEqualTo(originalCreatedAt); remove the redundant onUpdate_shouldNotModifyCreatedAt test so you only verify both that updatedAt advanced and createdAt stayed the same (use record.onCreate(), record.onUpdate(), record.getCreatedAt(), record.getUpdatedAt()).src/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordResponseTest.java (1)
163-173: Consider extracting reflection helpers into a shared test utility.
setPrivateField/callProtectedMethodare duplicated here and insrc/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordSummaryResponseTest.java(Line 112-122). A small shared helper would reduce duplication.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordResponseTest.java` around lines 163 - 173, Extract the duplicated reflection helpers into a single test utility class (e.g., ReflectTestUtils) exposing static methods setPrivateField(Object target, String fieldName, Object value) and callProtectedMethod(Object target, String methodName) that perform the same reflection steps and rethrow or wrap checked exceptions as RuntimeException; then replace the local private methods in MedicalRecordResponseTest and MedicalRecordSummaryResponseTest with calls to ReflectTestUtils.setPrivateField(...) and ReflectTestUtils.callProtectedMethod(...). Ensure the utility class is in a test sources package accessible to both tests and that method signatures and behavior match the originals.
🤖 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/dto/response/medicalrecord/MedicalRecordResponseTest.java`:
- Around line 108-115: The test from_shouldMapClosedAtWhenStatusIsClosed can
pass falsely when both closedAt values are null; update the test in
MedicalRecordResponseTest so it ensures a non-null closedAt is mapped by either
(a) set a concrete closedAt on the source record before calling
MedicalRecordResponse.from(record) (e.g., record.setClosedAt(...)) or (b) add an
assertion that response.closedAt() is not null and then
assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); reference the
test method from_shouldMapClosedAtWhenStatusIsClosed, the factory
MedicalRecordResponse.from, and the accessors response.closedAt() and
record.getClosedAt() when making the change.
---
Nitpick comments:
In
`@src/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordResponseTest.java`:
- Around line 163-173: Extract the duplicated reflection helpers into a single
test utility class (e.g., ReflectTestUtils) exposing static methods
setPrivateField(Object target, String fieldName, Object value) and
callProtectedMethod(Object target, String methodName) that perform the same
reflection steps and rethrow or wrap checked exceptions as RuntimeException;
then replace the local private methods in MedicalRecordResponseTest and
MedicalRecordSummaryResponseTest with calls to
ReflectTestUtils.setPrivateField(...) and
ReflectTestUtils.callProtectedMethod(...). Ensure the utility class is in a test
sources package accessible to both tests and that method signatures and behavior
match the originals.
In `@src/test/java/org/example/vet1177/entities/MedicalRecordEntityTest.java`:
- Around line 220-238: The two tests overlap: consolidate into a single test
(e.g., onUpdate_shouldRefreshUpdatedAtWithoutModifyingCreatedAt) that calls
record.onCreate(), captures Instant originalCreatedAt = record.getCreatedAt(),
waits or ensures time advances if needed, calls record.onUpdate(), then
assertThat(record.getUpdatedAt()).isAfter(originalCreatedAt) and
assertThat(record.getCreatedAt()).isEqualTo(originalCreatedAt); remove the
redundant onUpdate_shouldNotModifyCreatedAt test so you only verify both that
updatedAt advanced and createdAt stayed the same (use record.onCreate(),
record.onUpdate(), record.getCreatedAt(), record.getUpdatedAt()).
🪄 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: aa540896-2624-4cbe-9da1-309204d13851
📒 Files selected for processing (3)
src/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordResponseTest.javasrc/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordSummaryResponseTest.javasrc/test/java/org/example/vet1177/entities/MedicalRecordEntityTest.java
| void from_shouldMapClosedAtWhenStatusIsClosed() { | ||
| record.setStatus(RecordStatus.CLOSED); | ||
|
|
||
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | ||
|
|
||
| assertThat(response.status()).isEqualTo(RecordStatus.CLOSED); | ||
| assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); | ||
| } |
There was a problem hiding this comment.
closedAt test can pass on a false positive.
This test should also assert non-null closedAt; currently it passes if both values are null.
✅ Tighten assertion
`@Test`
void from_shouldMapClosedAtWhenStatusIsClosed() {
record.setStatus(RecordStatus.CLOSED);
MedicalRecordResponse response = MedicalRecordResponse.from(record);
assertThat(response.status()).isEqualTo(RecordStatus.CLOSED);
+ assertThat(record.getClosedAt()).isNotNull();
+ assertThat(response.closedAt()).isNotNull();
assertThat(response.closedAt()).isEqualTo(record.getClosedAt());
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| void from_shouldMapClosedAtWhenStatusIsClosed() { | |
| record.setStatus(RecordStatus.CLOSED); | |
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | |
| assertThat(response.status()).isEqualTo(RecordStatus.CLOSED); | |
| assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); | |
| } | |
| void from_shouldMapClosedAtWhenStatusIsClosed() { | |
| record.setStatus(RecordStatus.CLOSED); | |
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | |
| assertThat(response.status()).isEqualTo(RecordStatus.CLOSED); | |
| assertThat(record.getClosedAt()).isNotNull(); | |
| assertThat(response.closedAt()).isNotNull(); | |
| assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@src/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordResponseTest.java`
around lines 108 - 115, The test from_shouldMapClosedAtWhenStatusIsClosed can
pass falsely when both closedAt values are null; update the test in
MedicalRecordResponseTest so it ensures a non-null closedAt is mapped by either
(a) set a concrete closedAt on the source record before calling
MedicalRecordResponse.from(record) (e.g., record.setClosedAt(...)) or (b) add an
assertion that response.closedAt() is not null and then
assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); reference the
test method from_shouldMapClosedAtWhenStatusIsClosed, the factory
MedicalRecordResponse.from, and the accessors response.closedAt() and
record.getClosedAt() when making the change.
|
Closes #99 |
Summary
MedicalRecord-entiteten som täcker getters/setters, status- ochclosedAt-logik, attachment-hantering (add/remove/setAll, oföränderlig lista, null-skydd)samt lifecycle-hookarna
onCreate/onUpdate.MedicalRecordResponse.from(...)ochMedicalRecordSummaryResponse.from(...)med happy paths, null-hantering avassignedVetsamt sad paths förobligatoriska relationer (pet, owner, clinic, createdBy).
Comment-tester (AssertJ, reflection-helpers för att sätta id:n och trigga@PrePersist).Summary by CodeRabbit