Add DefaultLookupTest for lookupOptional NPE fix#12384
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a Maven Core regression test to ensure DefaultLookup.lookupOptional(...) continues to return Optional.empty() when the underlying PlexusContainer returns null (the behavior introduced by switching to Optional.ofNullable in #12336), preventing silent reintroduction of the prior NPE.
Changes:
- Introduces
DefaultLookupTestcovering bothlookupOptionaloverloads for thenull-return case. - Adds assertions for the “value present” case and for translation of
ComponentLookupExceptioncaused byNoSuchElementExceptiontoOptional.empty().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /** | ||
| * A {@link ComponentLookupException} whose cause is a {@link NoSuchElementException} signals an | ||
| * absent component and must be translated into an empty {@link Optional}. | ||
| */ | ||
| @Test | ||
| void lookupOptionalReturnsEmptyOnNoSuchElementCause() throws Exception { | ||
| ComponentLookupException cle = | ||
| new ComponentLookupException(new NoSuchElementException(), String.class.getName(), ""); | ||
| assertSame(NoSuchElementException.class, cle.getCause().getClass(), "test fixture precondition"); | ||
| when(container.lookup(String.class)).thenThrow(cle); | ||
|
|
||
| Optional<String> result = lookup.lookupOptional(String.class); | ||
|
|
||
| assertFalse(result.isPresent(), "expected empty Optional when component is absent"); | ||
| } |
0aa9008 to
0a65c49
Compare
gnodet
left a comment
There was a problem hiding this comment.
Review: Add DefaultLookupTest for lookupOptional NPE fix (master)
Nice work adding this regression guard — the test is well-structured and covers the key scenarios. A couple of minor suggestions below.
Findings
1. [Low / Suggestion] Missing test for the rethrow path
The production code has a branch where ComponentLookupException with a cause that is not NoSuchElementException rethrows as LookupException. The test covers the NoSuchElementException cause path but does not test that other causes are properly rethrown. This would round out coverage, but is not a blocker for this focused regression guard PR.
2. [Info] No [MNG-XXXX] prefix in commit message
Minor convention deviation — acceptable for a test-only addition (the original fix #12336 also had none).
Positive notes
- 6 well-structured JUnit 5 test methods covering both
lookupOptionaloverloads × 3 scenarios (null, present, NoSuchElementException) - Proper Mockito usage with existing project dependency
- Correct Apache License 2.0 header
- CI: all 22 checks pass
This review does not replace specialized tools such as CodeRabbit, Sourcery, or SonarCloud.
Reviewed with Claude Code on behalf of @gnodet. This review was generated by an AI agent and may contain inaccuracies; please verify all suggestions before applying.
The NPE fix for DefaultLookup.lookupOptional (apache#12336) — switching Optional.of to Optional.ofNullable so a null component lookup returns an empty Optional instead of throwing NullPointerException — shipped without a regression test. Add DefaultLookupTest in maven-core covering both lookupOptional overloads across four scenarios: null component -> empty Optional (the guarded NPE case), present component -> value, ComponentLookupException caused by NoSuchElementException -> empty, and any other cause -> rethrown as LookupException. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
0a65c49 to
e5f8155
Compare
|
Thanks for the review, @gnodet! Both points addressed: 1. Rethrow path — added coverage for a 2. Commit reference — added a Force-pushed ( |
What
Adds
DefaultLookupTest(maven-core) — the regression test that the NPE fix in #12336 shipped without.Why
#12336 changed
DefaultLookup.lookupOptional(...)to useOptional.ofNullableinstead ofOptional.of, so anullcomponent lookup returns an emptyOptionalinstead of throwingNullPointerException. Nothing currently guards that behavior — reverting the fix goes undetected by the suite.Test
DefaultLookupTestcovers bothlookupOptionaloverloads:null-> emptyOptional(the guarded NPE case)OptionalComponentLookupExceptionwhose cause isNoSuchElementException-> emptyVerified locally: green on current master; reverting
ofNullable->ofturns the two null-case tests red with the NPE fromjava.util.Optional.of. No production code change.