Skip to content

Conversation

@ps-mir
Copy link
Contributor

@ps-mir ps-mir commented Jan 21, 2026

JUnit5 executes the extension lifecycle per container class (Top or Nested). As a result afterAll is called immediately post execution of one nested class.

@Override
public void afterAll(ExtensionContext context) {
GlobalOpenTelemetry.resetForTest();
openTelemetry.close();
}

Making it unavailable for other Nested classes followed by failures.

As discussed in #7919 (comment), this solution preserves the Extension until afterAll is called at the top level.

We can look into more elaborate cleanup via ExtensionContext.Store.CloseableResource if required in future.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 21, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Jan 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.15%. Comparing base (aa2ca12) to head (bf52143).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #7999      +/-   ##
============================================
- Coverage     90.16%   90.15%   -0.02%     
- Complexity     7475     7476       +1     
============================================
  Files           836      836              
  Lines         22551    22552       +1     
  Branches       2224     2225       +1     
============================================
- Hits          20333    20331       -2     
- Misses         1515     1517       +2     
- Partials        703      704       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ps-mir
Copy link
Contributor Author

ps-mir commented Jan 21, 2026

@jkwatson @austince I expected this commit to fail as Test reproduces the issue. But it seems its not part of CI.

./gradlew :sdk:testing:build
Parallel Configuration Cache is an incubating feature.
Calculating task graph as configuration cache cannot be reused because cached version information for io.opentelemetry:opentelemetry-api:latest.release has expired.
Kotlin does not yet support 25 JDK target, falling back to Kotlin JVM_24 JVM target

> Task :buildSrc:compileKotlin UP-TO-DATE
Kotlin does not yet support 25 JDK target, falling back to Kotlin JVM_24 JVM target

> Task :sdk:testing:test

OpenTelemetryExtensionNestedTest > FirstNestedClass > recordSpan() FAILED
    java.lang.AssertionError: 
    Expected size: 1 but was: 0 in:
    []
        at io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtensionNestedTest$FirstNestedClass.recordSpan(OpenTelemetryExtensionNestedTest.java:30)

80 tests completed, 1 failed

> Task :sdk:testing:test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':sdk:testing:test'.

Reproduces issue open-telemetry#7919 where extension's `afterAll` hook is called
prematurely after one nested test class execution completes, making the
extension unusable for other nested test class.
The afterAll lifecycle callback requires a valid ExtensionContext to
determine if cleanup should occur at the top level. The test was
previously passing null, causing NPE after the nested class handling
logic was added.
The nested class path (where close() is not called) was not covered,
causing codecov patch coverage to fall below the 80% threshold.

Added a test for NestedClass scenario to validate that sdk remains
functional when afterAll is called post Nested class test.
@ps-mir ps-mir force-pushed the sdk/testing/junit5-extension-nested-cleanup branch from d0c2c3f to bf52143 Compare January 21, 2026 16:52
@ps-mir
Copy link
Contributor Author

ps-mir commented Jan 21, 2026

Modified Extension tests to include mocked ExtensionContext. It looks ok now.

@ps-mir ps-mir marked this pull request as ready for review January 21, 2026 17:21
@ps-mir ps-mir requested a review from a team as a code owner January 21, 2026 17:21
Copy link
Contributor

@jkwatson jkwatson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jkwatson jkwatson merged commit a8a69be into open-telemetry:main Jan 21, 2026
27 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Jan 21, 2026

Thank you for your contribution @ps-mir! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

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.

2 participants