Test: add unit tests for VetResponse mapping#180
Conversation
Added VetResponseTest to verify DTO mapping from Vet entity Covered mapping of all fields including nested User and Clinic data Added test case for null clinic scenario Used reflection to simulate JPA-generated IDs for User and Vet (@mapsid) Ensures correct handling of active status and optional fields
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 2 minutes and 41 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds a new JUnit 5 package-private test class Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
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 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 (1)
src/test/java/org/example/vet1177/dto/response/vet/VetResponseTest.java (1)
27-31: Consider extracting reflection field setup into a small helper.The reflective ID setup is repeated and brittle; a helper keeps tests shorter and easier to maintain.
Suggested refactor
class VetResponseTest { + private static void setField(Object target, String fieldName, Object value) throws Exception { + Field field = target.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + field.set(target, value); + } @@ - Field idField = User.class.getDeclaredField("id"); - idField.setAccessible(true); - idField.set(user, userId); + setField(user, "id", userId); @@ - Field vetUserIdField = Vet.class.getDeclaredField("userId"); - vetUserIdField.setAccessible(true); - vetUserIdField.set(vet, userId); + setField(vet, "userId", userId); @@ - Field idField = User.class.getDeclaredField("id"); - idField.setAccessible(true); - idField.set(user, userId); + setField(user, "id", userId);Also applies to: 39-41, 69-71
🤖 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/vet/VetResponseTest.java` around lines 27 - 31, Extract the repeated reflection logic that sets private id fields into a small reusable helper in the test class (e.g., a private static method like setPrivateField(Object target, String fieldName, Object value) or setId(Object target, Long id)); replace the duplicated blocks that create Field idField, call setAccessible(true) and idField.set(...) for User and any other classes with calls to that helper (references: User class, the idField variable usage in VetResponseTest). Ensure the helper handles NoSuchFieldException/IllegalAccessException by wrapping or rethrowing as a runtime exception so tests fail clearly.
🤖 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/vet/VetResponseTest.java`:
- Around line 68-85: The test for null-clinic handling omits setting vet.userId
so VetResponse.from(vet) may read a null ID; update the test to set the vet's
userId (as done in the first test) before calling VetResponse.from(vet) — for
example, populate the User.id via reflection (Field idField on User) and then
set vet.setUserId(userId) or otherwise set vet.userId on the Vet instance; then
assert the response contains the expected userId while still asserting
clinicName() is null and isActive() is false to ensure ID mapping is validated
alongside null-clinic behavior.
---
Nitpick comments:
In `@src/test/java/org/example/vet1177/dto/response/vet/VetResponseTest.java`:
- Around line 27-31: Extract the repeated reflection logic that sets private id
fields into a small reusable helper in the test class (e.g., a private static
method like setPrivateField(Object target, String fieldName, Object value) or
setId(Object target, Long id)); replace the duplicated blocks that create Field
idField, call setAccessible(true) and idField.set(...) for User and any other
classes with calls to that helper (references: User class, the idField variable
usage in VetResponseTest). Ensure the helper handles
NoSuchFieldException/IllegalAccessException by wrapping or rethrowing as a
runtime exception so tests fail clearly.
🪄 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: 0c3dab23-e515-4b22-be58-5282e0aa0be8
📒 Files selected for processing (1)
src/test/java/org/example/vet1177/dto/response/vet/VetResponseTest.java
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/vet/VetResponseTest.java`:
- Around line 21-24: The test instantiates User with new User() but the entity
only exposes a parameterized constructor, so update VetResponseTest to construct
User using the available User(...) constructor (provide a valid role and
password fixture values consistent with
src/main/java/org/example/vet1177/entities/User.java) instead of calling the
no-arg constructor; after construction, retain the existing setActive(...) and
setClinic(...) calls (and any other setters at lines 21 and 56-59) to preserve
subsequent test setup.
- Around line 18-20: The test creates a Clinic and calls a non-existent mutator
clinic.setName(...); update VetResponseTest to instantiate Clinic using the
available constructor instead of calling setName — e.g., replace the no-arg new
Clinic() + setName call with a constructor call that sets the name (new
Clinic("Happy Pets Clinic")) or otherwise use the Clinic constructor signature
provided by the Clinic class; adjust the test's Clinic creation in
VetResponseTest accordingly.
🪄 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: 01bad61c-8449-457c-88fe-cb2ffaba2666
📒 Files selected for processing (1)
src/test/java/org/example/vet1177/dto/response/vet/VetResponseTest.java
Added VetResponseTest to verify DTO mapping from Vet entity Covered mapping of all fields including nested User and Clinic data Added test case for null clinic scenario
Used reflection to simulate JPA-generated IDs for User and Vet (@mapsid) Ensures correct handling of active status and optional fields
Summary by CodeRabbit