test: add unit tests for PetEntity#161
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded a JUnit 5 test class that validates the Pet entity's getters/setters, full-constructor initialization (including nullable Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 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 (3)
src/test/java/org/example/vet1177/entities/PetEntityTest.java (3)
10-10: Consider using the standard AssertJ import.
Assertionsis the conventional entry point for AssertJ, providing both object and collection assertions.Suggested change
-import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.Assertions.assertThat;🤖 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/PetEntityTest.java` at line 10, Replace the nonstandard static import AssertionsForClassTypes.assertThat used in PetEntityTest with the conventional AssertJ entry point by importing Assertions.assertThat (use a static import of org.assertj.core.api.Assertions.assertThat or import org.assertj.core.api.Assertions and call Assertions.assertThat) so the test uses the standard AssertJ API; update the import statement referencing assertThat accordingly in PetEntityTest.java.
109-116: Strengthen this test to verifyupdatedAtactually changes.After
onCreate(),updatedAtis already non-null, soassertThat(pet.getUpdatedAt()).isNotNull()always passes trivially. Consider verifying thatupdatedAtis refreshed to a new value.Suggested improvement
`@Test` void onUpdate_shouldRefreshUpdatedAt() { pet.onCreate(); + Instant originalUpdatedAt = pet.getUpdatedAt(); pet.onUpdate(); - assertThat(pet.getUpdatedAt()).isNotNull(); + assertThat(pet.getUpdatedAt()).isNotNull(); + assertThat(pet.getUpdatedAt()).isAfterOrEqualTo(originalUpdatedAt); }🤖 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/PetEntityTest.java` around lines 109 - 116, The test in PetEntityTest currently only checks non-null updatedAt after calling pet.onUpdate(), which always passes because pet.onCreate() already sets it; update the test to capture the timestamp after pet.onCreate() (e.g., Instant old = pet.getUpdatedAt()), then call pet.onUpdate(), and assert that pet.getUpdatedAt() is not equal to old and is after/greater than old (you can introduce a tiny delay or use time assertions) to ensure onUpdate() actually refreshes the timestamp; reference methods: onCreate(), onUpdate(), and getUpdatedAt() on the pet instance.
12-175: Missing validation constraint tests from linked issue#142.The linked issue requires testing Bean Validation annotations, but these are not covered:
@NotBlankonnameandspecies(reject null/blank)@NotNullondateOfBirth@PositiveonweightKgConsider adding tests using a
Validatorinstance to verify constraints:import jakarta.validation.Validation; import jakarta.validation.Validator; `@Test` void name_shouldRejectBlankValue() { Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); pet.setName(""); pet.setSpecies("hund"); pet.setDateOfBirth(LocalDate.now()); pet.setWeightKg(new BigDecimal("5.0")); var violations = validator.validate(pet); assertThat(violations).anyMatch(v -> v.getPropertyPath().toString().equals("name")); }Do you want me to generate the complete validation test suite, or open an issue to track this?
🤖 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/PetEntityTest.java` around lines 12 - 175, Add Bean Validation tests to PetEntityTest that use a Validator (Validation.buildDefaultValidatorFactory().getValidator()) to assert constraint violations for Pet fields: create tests named name_shouldRejectBlankValue and species_shouldRejectBlankValue that set pet.setName("") / pet.setSpecies("") respectively while populating other required fields (dateOfBirth, weightKg, species/name) with valid values and assert violations contain propertyPath "name" or "species"; add dateOfBirth_shouldRejectNull that leaves dateOfBirth null and asserts a violation for "dateOfBirth"; and add weightKg_shouldRejectNonPositive that sets weightKg to zero or negative and asserts a violation for "weightKg". Ensure each test uses the same Validator instance and checks violations.stream().anyMatch(v -> v.getPropertyPath().toString().equals("<fieldName>")).
🤖 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/entities/PetEntityTest.java`:
- Around line 29-58: In PetEntityTest, fix the typos in the test method names so
test reports/readability are correct: rename
setSpeices_shouldStoreAndReturnCorrectValue to
setSpecies_shouldStoreAndReturnCorrectValue,
setBread_shouldStoreAndReturnCorrectValue to
setBreed_shouldStoreAndReturnCorrectValue,
setDateOfBirth_shouldStorAndReturnCorrectValue to
setDateOfBirth_shouldStoreAndReturnCorrectValue, and
setWeigthKg_shouldStoreAndReturnCorrectValue to
setWeightKg_shouldStoreAndReturnCorrectValue; update only the method names in
the PetEntityTest class so the tests and assertions (pet.setSpecies,
pet.setBreed, pet.setDateOfBirth, pet.setWeightKg) remain unchanged.
---
Nitpick comments:
In `@src/test/java/org/example/vet1177/entities/PetEntityTest.java`:
- Line 10: Replace the nonstandard static import
AssertionsForClassTypes.assertThat used in PetEntityTest with the conventional
AssertJ entry point by importing Assertions.assertThat (use a static import of
org.assertj.core.api.Assertions.assertThat or import
org.assertj.core.api.Assertions and call Assertions.assertThat) so the test uses
the standard AssertJ API; update the import statement referencing assertThat
accordingly in PetEntityTest.java.
- Around line 109-116: The test in PetEntityTest currently only checks non-null
updatedAt after calling pet.onUpdate(), which always passes because
pet.onCreate() already sets it; update the test to capture the timestamp after
pet.onCreate() (e.g., Instant old = pet.getUpdatedAt()), then call
pet.onUpdate(), and assert that pet.getUpdatedAt() is not equal to old and is
after/greater than old (you can introduce a tiny delay or use time assertions)
to ensure onUpdate() actually refreshes the timestamp; reference methods:
onCreate(), onUpdate(), and getUpdatedAt() on the pet instance.
- Around line 12-175: Add Bean Validation tests to PetEntityTest that use a
Validator (Validation.buildDefaultValidatorFactory().getValidator()) to assert
constraint violations for Pet fields: create tests named
name_shouldRejectBlankValue and species_shouldRejectBlankValue that set
pet.setName("") / pet.setSpecies("") respectively while populating other
required fields (dateOfBirth, weightKg, species/name) with valid values and
assert violations contain propertyPath "name" or "species"; add
dateOfBirth_shouldRejectNull that leaves dateOfBirth null and asserts a
violation for "dateOfBirth"; and add weightKg_shouldRejectNonPositive that sets
weightKg to zero or negative and asserts a violation for "weightKg". Ensure each
test uses the same Validator instance and checks violations.stream().anyMatch(v
-> v.getPropertyPath().toString().equals("<fieldName>")).
🪄 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: 6434f252-6326-4361-8716-08f7fb76fa22
📒 Files selected for processing (1)
src/test/java/org/example/vet1177/entities/PetEntityTest.java
Closes #142
Täcker:
Summary by CodeRabbit