Skip to content

test: add unit tests for UserService#178

Merged
lindaeskilsson merged 4 commits into
mainfrom
test/UserServiceTest
Apr 13, 2026
Merged

test: add unit tests for UserService#178
lindaeskilsson merged 4 commits into
mainfrom
test/UserServiceTest

Conversation

@lindaeskilsson

@lindaeskilsson lindaeskilsson commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Closes #148

Täcker createUser, getByEmail, getUserEntityById, getById, getAllUsers, updateUser och deleteUser inklusive affärsregler för klinik-koppling, email-validering och blockering av radering vid kopplade resurser.

Summary by CodeRabbit

  • Tests
    • Added comprehensive JUnit test suite for user management covering creation, update, deletion, role-specific flows, validation and error scenarios, uniqueness checks, and persistence-layer exception mapping; includes helper utilities for deterministic test data.

@coderabbitai

coderabbitai Bot commented Apr 9, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 70614d17-4ce4-4e17-befa-87e2131d132f

📥 Commits

Reviewing files that changed from the base of the PR and between dceac9a and ccfdf95.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/services/UserServiceTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/test/java/org/example/vet1177/services/UserServiceTest.java

📝 Walkthrough

Walkthrough

Adds a new JUnit 5 test suite UserServiceTest that exercises create, update, delete, and read flows of UserService, including numerous business-rule and error scenarios using Mockito mocks, AssertJ assertions, and reflection-based id injection. (≈492 lines added)

Changes

Cohort / File(s) Summary
UserServiceTest Suite
src/test/java/org/example/vet1177/services/UserServiceTest.java
New comprehensive test class covering createUser, updateUser, deleteUser, getById, getByEmail, and getAllUsers scenarios: role/clinic validation, email-uniqueness checks, repository exception mappings (DataIntegrityViolation → BusinessRuleException), and deletion constraint checks. Includes reflection helpers and request builders.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

test

Suggested reviewers

  • annikaholmqvist94
  • TatjanaTrajkovic

Poem

🐰
Hops and tests in tidy rows,
I mock the world where vet-work grows.
Owners, vets, and clinics fine,
I assert each rule, line by line.
Cheers — a rabbit’s QA rhyme!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.53% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'test: add unit tests for UserService' clearly and directly describes the main change - adding a new test class with comprehensive unit tests for the UserService class.
Linked Issues check ✅ Passed The pull request comprehensively covers all objectives from issue #148: createUser scenarios (OWNER/VET with/without clinic, duplicate email), getById/getByEmail/getAllUsers retrieval methods, updateUser for name/email/clinic changes, and deleteUser with all constraint validations (pets, medical records, assigned vet, created/updated records).
Out of Scope Changes check ✅ Passed The pull request contains only test code additions directly aligned with issue #148 requirements; all changes are in-scope test implementations for UserService functionality with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/UserServiceTest

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/services/UserServiceTest.java`:
- Around line 75-370: Add missing read-path unit tests for
userService.getByEmail, userService.getUserEntityById, userService.getById and
userService.getAllUsers: for getByEmail mock userRepository.findByEmail to
return Optional.of(user) and assert returned UserResponse fields and that
repository was invoked, plus a test where findByEmail returns Optional.empty()
and assert ResourceNotFoundException; for getUserEntityById mock
userRepository.findById to return Optional.of(user) and assert the returned User
entity, plus a test for Optional.empty() -> ResourceNotFoundException; for
getById mock userRepository.findById to return Optional.of(user) and assert
UserResponse mapping and unknown id -> ResourceNotFoundException; for
getAllUsers mock userRepository.findAll (or appropriate paged method) to return
a list of users and assert userService.getAllUsers returns the mapped list and
verify repository interaction. Ensure you use the same naming/style as existing
tests and reuse the existing fixtures (ownerUser/vetUser/userId/clinicId).
- Around line 78-91: The test createUser_owner_withoutClinic_returnsResponse
currently stubs userRepository.save(...) so it can't detect if raw passwords are
persisted; modify the test in UserServiceTest to capture the User passed to
userRepository.save (use an ArgumentCaptor<User> or verify with an argument
matcher) and assert that the saved User.password is not the plain request
password and is encoded by the service (use the
PasswordEncoder.matches(rawPassword, saved.getPassword()) or equivalent) while
keeping the existing assertions on response and verify(userRepository).save(...)
to ensure the password-hashing contract enforced by createUser is validated.
🪄 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: 85ef67d0-5cc4-47fe-afd1-c12c896a3fc0

📥 Commits

Reviewing files that changed from the base of the PR and between 5a74e27 and dceac9a.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/services/UserServiceTest.java

Comment thread src/test/java/org/example/vet1177/services/UserServiceTest.java
Comment thread src/test/java/org/example/vet1177/services/UserServiceTest.java

@TatjanaTrajkovic TatjanaTrajkovic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ser bra ut!

@lindaeskilsson
lindaeskilsson merged commit 77c3379 into main Apr 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test: UserServiceTest – CRUD-logik och affärsregler

2 participants