[test-improver] Add 27 unit tests for UserService using Mockito#59
Draft
github-actions[bot] wants to merge 1 commit into
Draft
[test-improver] Add 27 unit tests for UserService using Mockito#59github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
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>
14 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 Test Improver — automated AI assistant focused on improving tests.
Goal & Rationale
UserServicehad ~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 mockedUserRepository. Each test is isolated — no shared state between tests.Tests Added (27)
createUserauthenticategetUserupdateUserrequestPasswordResetresetPasswordsearchUsersgetUserDisplayNamegetActiveUsersByRoledeactivateUsersBug Pin: Role Escalation (Security)
updateUser_roleEscalationBugPin_anyUserCanBecomeAdmindocuments thatupdateUser()accepts arolefield 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,
UserServicehad ~1% instruction coverage (Spring Boot startup touches some paths). After:Exact numbers depend on whether JaCoCo PR #47 is merged; the estimate is based on reading all branches.
Trade-offs
@WebMvcTestPRs@SpringBootTestoverhead — each test class runs in isolation under 1 secondReproducibility
Test Status
Pre-existing failures (not caused by this PR):
DateUtilsTest.testGetQuarter— Bug 🔴 [BUG] getQuarter() returns 0-3 instead of 1-4 (off-by-one bug) #6 (off-by-one)TaskServiceTest.testGetTaskStatistics— Bug 🔴 [BUG] ArithmeticException: Division by zero on empty project dashboard #3 (division by zero)Add this agentic workflows to your repo
To install this agentic workflow, run