Skip to content

Fix NPE in ServletJwtSessionModuleTest due to commented-out mock stub#168

Merged
vharseko merged 2 commits intomasterfrom
copilot/fix-nullpointerexception-in-jwt-session-test
Mar 31, 2026
Merged

Fix NPE in ServletJwtSessionModuleTest due to commented-out mock stub#168
vharseko merged 2 commits intomasterfrom
copilot/fix-nullpointerexception-in-jwt-session-test

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodNotExpired was throwing a NullPointerException at line 521 because the stub for messageInfo.getResponseMessage() was accidentally commented out, causing Mockito to return null when production code called it.

Change

  • Restore mock stub in ServletJwtSessionModuleTest (line 498):
// Before (broken)
given(messageInfo.getRequestMessage()).willReturn(request);
//given(messageInfo.getResponseMessage()).willReturn(response);  ← commented out
given(messageInfo.getMap()).willReturn(map);

// After (fixed)
given(messageInfo.getRequestMessage()).willReturn(request);
given(messageInfo.getResponseMessage()).willReturn(response);
given(messageInfo.getMap()).willReturn(map);

The sibling test shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodExpired already had this stub correctly in place and was passing.

Original prompt

Problem

The CI job for jwt-session-module is failing due to a NullPointerException in the following test:

Test: org.forgerock.jaspi.modules.session.jwt.ServletJwtSessionModuleTest.shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodNotExpired
File: commons/auth-filters/authn-filter/jaspi-modules/jwt-session-module/src/test/java/org/forgerock/jaspi/modules/session/jwt/ServletJwtSessionModuleTest.java
Line: 521

Root Cause

In the test method shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodNotExpired, the Mockito stub for messageInfo.getResponseMessage() is commented out at line 498:

given(messageInfo.getRequestMessage()).willReturn(request);
//given(messageInfo.getResponseMessage()).willReturn(response);  // ← BUG: commented out
given(messageInfo.getMap()).willReturn(map);

When the production code calls messageInfo.getResponseMessage(), Mockito returns null (no stub defined), which causes a NullPointerException when validateRequest subsequently uses the response object.

The identical pattern in the sibling test shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodExpired (around line 578) has this stub correctly in place and passes successfully.

Fix

Uncomment line 498 in ServletJwtSessionModuleTest.java to restore the mock stub:

given(messageInfo.getRequestMessage()).willReturn(request);
given(messageInfo.getResponseMessage()).willReturn(response); // restore this line
given(messageInfo.getMap()).willReturn(map);

This ensures messageInfo.getResponseMessage() returns the mocked HttpServletResponse instead of null, eliminating the NullPointerException.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Please find a solution for this failing job. Use the logs, job definition, and any referenced files where the failure occurred. Keep your response focused on the solution and include code suggestions when appropriate.

Assistant: The root cause is clear. Let me fetch the failing test file:

Assistant: The failing test is:

ServletJwtSessionModuleTest.shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodNotExpired at line 521 with a NullPointerException.


Root Cause

Look at line 498 — the stub for messageInfo.getResponseMessage() is commented out:

given(messageInfo.getRequestMessage()).willReturn(request);
//given(messageInfo.getResponseMessage()).willReturn(response);  // ← BUG: commented out
given(messageInfo.getMap()).willReturn(map);

When the production code under test calls messageInfo.getResponseMessage(), Mockito returns null (since no stub is defined), which causes a NullPointerException at line 521 when validateRequest tries to use the response object.

Compare this with the similar test shouldValidateRequestWhenJwtSessionCookiePresentAndValidCoolOffPeriodExpired (line 578–580), where the stub is correctly in place:

given(messageInfo.getRequestMessage()).willReturn(request);
given(messageInfo.getResponseMessage()).willReturn(response);  // ← correctly present
given(messageInfo.getMap()).willReturn(map);

Fix

Uncomment line 498 in ServletJwtSessionModuleTest.java:

given(messageInfo.getRequestMessage()).willReturn(request);
given(messageInfo.getResponseMessage()).willReturn(response); // ← restore this line
given(messageInfo.getMap()).willReturn(map);

This restores the mock stub so messageInfo.getResponseMessage() returns the mocked HttpServletResponse instead of null, eliminating the NullPointerException.

User: make PR

Assistant: I've submitted the request to create a pull request in OpenIdentityPlatform/commons targeting the master branch. We're waiting for your response to the tool call dialog to continue.

Once confirmed, the PR will uncomment line 498 in ServletJwtSessionModuleTest.java, restoring the missing given(messageInfo.getResponseMessage()).willReturn(response); stub that was causing the NullPointerException in the failing test.

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix null pointer exception in JWT session test Fix NPE in ServletJwtSessionModuleTest due to commented-out mock stub Mar 31, 2026
Copilot AI requested a review from vharseko March 31, 2026 07:14
@vharseko vharseko marked this pull request as ready for review March 31, 2026 08:10
@vharseko vharseko requested a review from maximthomas March 31, 2026 08:10
@vharseko vharseko merged commit a6acd5f into master Mar 31, 2026
30 checks passed
@vharseko vharseko deleted the copilot/fix-nullpointerexception-in-jwt-session-test branch March 31, 2026 08:20
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.

3 participants