Skip to content

[test-improver] Add model unit tests: User.getDisplayName() logic and Task.toString() NPE bugs#74

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
test-assist/model-unit-tests-f0ff159dad6ffe66
Draft

[test-improver] Add model unit tests: User.getDisplayName() logic and Task.toString() NPE bugs#74
github-actions[bot] wants to merge 1 commit into
mainfrom
test-assist/model-unit-tests-f0ff159dad6ffe66

Conversation

@github-actions

Copy link
Copy Markdown

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

Goal & Rationale

Two model classes had untested logic with real bugs:

  1. User.getDisplayName() — a 3-branch method with surprising partial-name behaviour: if firstName is set but lastName is null (or vice versa), the set name is silently discarded and the method falls back to full_name/username. A caller who sets firstName but forgets lastName gets no error; their data is lost.

  2. Task.toString() — two documented NPEs: assignee_id.toString() (line 122) and due_date.trim() (line 123) both dereference fields without null checks. Any Task constructed via the no-arg JPA constructor will throw when toString() is called (logging, debugger, etc.).

Approach

Pure no-Spring JUnit 5 tests (@ExtendWith(MockitoExtension.class) not even needed — plain POJO construction). Runs in < 1 s with no context startup.

UserModelTest (10 tests):

Test Branch
getDisplayName_bothFirstAndLastName_returnsCombined First+Last → "John Smith"
getDisplayName_noFirstOrLastName_usesFullName full_name fallback
getDisplayName_noNamesAtAll_usesUsername username fallback
getDisplayName_firstNameSetButLastNameNull_fallsBackToFullName Bug pin
getDisplayName_firstNameSetLastNameNullNoFullName_fallsBackToUsername Bug pin
getDisplayName_lastNameSetButFirstNameNull_fallsBackToUsername Bug pin
getDisplayName_firstAndLastNamePrecedeFullName Precedence
getDisplayName_fullNamePrecedesUsername Precedence
getDisplayName_emptyFirstAndLastName_returnsSpaceSeparated Edge case
getDisplayName_defaultConstructor_noNPE Null safety

TaskModelTest (7 tests):

Test Branch
shortConstructor_setsFieldsCorrectly Short constructor
defaultConstructor_allFieldsDefaultToNull Default constructor
toString_nullAssigneeId_throwsNPE Bug pin
toString_nullDueDate_throwsNPE Bug pin
toString_defaultConstructorFields_throwsNPE Bug pin
toString_allRequiredFieldsSet_noException Happy path
attachmentUrls_initializedAsEmptyList Initial state

Coverage Impact

Tests are pure POJO — no Spring context, no DB. Adds coverage for User.getDisplayName() (all 3 branches) and Task.toString() (NPE paths).

Trade-offs

  • Bug-pin tests intentionally assert the current buggy behaviour (assertThrows(NPE)) so that fixing the bug requires updating the tests — making the fix visible.
  • No Spring context → no DataInitializer race conditions.

Reproducibility

mvn test -Dtest="UserModelTest,TaskModelTest" -Dmaven.repo.local=/tmp/.m2 -B

Test Status

✅ 17/17 tests pass (10 UserModelTest + 7 TaskModelTest)
✅ Existing test suite unaffected (pre-existing failures on testGetQuarter and testGetTaskStatistics are unchanged)

Generated by Test Improver · 88.6 AIC · ⌖ 9.46 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

…g() bugs

UserModelTest (10 tests):
- 3 happy-path tests for all getDisplayName() branches
- 3 bug-pin tests: partial name (firstName but no lastName, or vice versa) is
  silently ignored — caller's name data is lost, method falls back to
  full_name/username without warning
- 2 precedence tests (firstName+lastName beats full_name; full_name beats username)
- 2 edge cases (empty strings, default constructor returns null without NPE)

TaskModelTest (7 tests):
- 2 constructor tests (short and default constructors)
- 3 bug-pin tests: Task.toString() throws NPE when assignee_id or due_date is null
  (calls assignee_id.toString() and due_date.trim() without null checks)
- 1 positive test: toString() succeeds when all required fields are set
- 1 test: attachmentUrls initialised as empty list (not null)

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