Skip to content

[test-improver] Add 27 unit tests for UserService using Mockito#59

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
test-assist/userservice-unit-tests-55b590f46650f5a7
Draft

[test-improver] Add 27 unit tests for UserService using Mockito#59
github-actions[bot] wants to merge 1 commit into
mainfrom
test-assist/userservice-unit-tests-55b590f46650f5a7

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

🤖 Test Improver — automated AI assistant focused on improving tests.

Goal & Rationale

UserService had ~1% code coverage — the entire authentication and user-management service was effectively untested. This is the highest-risk service in the codebase: it handles login, password reset, and user creation.

This PR adds 27 pure Mockito unit tests covering all public methods, with no Spring context or database — tests run in under 1 second.

Approach

Used @ExtendWith(MockitoExtension.class) with a mocked UserRepository. Each test is isolated — no shared state between tests.

Tests Added (27)

Area Tests
createUser success, duplicate username, duplicate email
authenticate valid credentials, unknown user, inactive account, wrong password, failed-attempt increment
getUser found, not found
updateUser not found, null fields preserved, role escalation bug pin
requestPasswordReset unknown email, token returned + persisted
resetPassword invalid token, token cleared + password updated
searchUsers blank keyword → empty, null keyword → empty, valid keyword delegates
getUserDisplayName not found, first+last, full_name fallback, username fallback
getActiveUsersByRole filters inactive and deleted users
deactivateUsers returns count, skips non-existent users

Bug Pin: Role Escalation (Security)

updateUser_roleEscalationBugPin_anyUserCanBecomeAdmin documents that updateUser() accepts a role field change with no authorization check — any caller can set their own role to "admin". This test pins the current behaviour so a future fix is visible in the test suite.

Coverage Impact

Before this PR, UserService had ~1% instruction coverage (Spring Boot startup touches some paths). After:

Metric UserService Before UserService After (est.)
Instructions ~1% ~80%+
Branches ~0% ~80%+
Methods 0/11 ~10/11

Exact numbers depend on whether JaCoCo PR #47 is merged; the estimate is based on reading all branches.

Trade-offs

  • Tests use Mockito, matching the pattern established in the controller @WebMvcTest PRs
  • No @SpringBootTest overhead — each test class runs in isolation under 1 second
  • Role escalation bug pin will need updating once the bug is fixed

Reproducibility

mvn test -Dtest=UserServiceTest -B
# All 27 tests should pass in ~1s
mvn test -B
# Full suite: 65 tests, 1 failure, 1 error (both pre-existing, not caused by this PR)

Test Status

Tests run: 27, Failures: 0, Errors: 0, Skipped: 0

Pre-existing failures (not caused by this PR):

Generated by Test Improver · 129.7 AIC · ⌖ 12.1 AIC · ⊞ 10.9K ·
Comment /test-assist to run again

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/test-improver.md@1c6668b751c51af8571f01204ceffb19362e0f66

Covers all UserService methods with pure Mockito unit tests (no Spring
context, no database). Tests run in <1s.

Test categories:
- createUser: success, duplicate username, duplicate email
- authenticate: valid credentials, unknown user, inactive account,
  wrong password, failed-attempt counter increment
- getUser: found, not found
- updateUser: not found, null fields preserved, role escalation bug pin
- requestPasswordReset: unknown email, token returned and persisted
- resetPassword: invalid token, token cleared + password updated
- searchUsers: blank/null keyword returns empty, valid keyword delegates
- getUserDisplayName: not found, first+last name, full_name fallback,
  username fallback
- getActiveUsersByRole: filters inactive and deleted users
- deactivateUsers: returns count, skips non-existent users

Bug pins:
- updateUser_roleEscalationBugPin: documents that any user can change
  their own role to admin with no authorization check

Pre-existing test failures (not caused by this PR):
- DateUtilsTest.testGetQuarter (Bug #6)
- TaskServiceTest.testGetTaskStatistics (Bug #3)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants