Skip to content

Use TestInstance.Lifecycle.PER_CLASS in subclasses too#18750

Open
trask wants to merge 1 commit into
open-telemetry:mainfrom
trask:fix-play-ws-autocleanup-guidelines
Open

Use TestInstance.Lifecycle.PER_CLASS in subclasses too#18750
trask wants to merge 1 commit into
open-telemetry:mainfrom
trask:fix-play-ws-autocleanup-guidelines

Conversation

@trask
Copy link
Copy Markdown
Member

@trask trask commented May 14, 2026

Subclasses already inherit TestInstance.Lifecycle.PER_CLASS from their parent, but (a) this is easy to miss since you have to look at the parent to know this, and (b) we are still using static state in many of them.

This PR establishes the coding guidelines / pattern.

Will follow-up afterwards to apply to remaining locations.

Extracted from #18741

@trask trask marked this pull request as ready for review May 15, 2026 18:54
Copilot AI review requested due to automatic review settings May 15, 2026 18:54
@trask trask requested a review from a team as a code owner May 15, 2026 18:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Converts the play-ws Play WS client test base classes from static state to instance state under @TestInstance(Lifecycle.PER_CLASS), and codifies the corresponding pattern in the module-cleanup agent and shared testing knowledge docs.

Changes:

  • Add @TestInstance(Lifecycle.PER_CLASS) to PlayWsClientBaseTest and its four concrete subclasses; convert static fixture fields and @BeforeAll methods to instance form, and make the AutoCleanupExtension an instance @RegisterExtension field.
  • Document the abstract-base + subclass PER_CLASS lifecycle pattern (with instance AutoCleanupExtension) in testing-general-patterns.md and the module-cleanup agent instructions.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
instrumentation/play/play-ws/play-ws-common-1.0/testing/.../PlayWsClientBaseTest.java Apply PER_CLASS, convert fixtures and AutoCleanupExtension to instance scope.
instrumentation/play/play-ws/play-ws-common-1.0/testing/.../PlayScalaWsClientBaseTest.java Repeat PER_CLASS on subclass; convert fields and helpers to instance.
instrumentation/play/play-ws/play-ws-common-1.0/testing/.../PlayScalaStreamedWsClientBaseTest.java Same conversion for the Scala streamed variant.
instrumentation/play/play-ws/play-ws-common-1.0/testing/.../PlayJavaWsClientBaseTest.java Same conversion for the Java WS variant.
instrumentation/play/play-ws/play-ws-common-1.0/testing/.../PlayJavaStreamedWsClientBaseTest.java Same conversion for the Java streamed variant.
.github/agents/module-cleanup.agent.md Add guidance for abstract-base PER_CLASS + instance AutoCleanupExtension pattern.
.github/agents/knowledge/testing-general-patterns.md Document the same pattern with rationale against shared static AutoCleanupExtension.

initialized earlier (e.g. a still-running test container).
- In abstract test bases that are inherited by multiple concrete test classes,
use `@TestInstance(Lifecycle.PER_CLASS)`, instance fixture fields, and an
instance `@RegisterExtension AutoCleanupExtension cleanup`. Register
Copy link
Copy Markdown
Contributor

@laurit laurit May 18, 2026

Choose a reason for hiding this comment

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

intellij warns when @RegisterExtension (at least for InstrumentationExtension) is used on a non-static field. https://youtrack.jetbrains.com/projects/IDEA/issues/IDEA-362874/JUnit-5-false-positive-extension-should-be-registered-at-the-class-level explains that if you use a non-static field then beforeAll and afterAll callbacks won't work. Idk maybe this isn't an issue with @TestInstance(Lifecycle.PER_CLASS)

import play.libs.ws.StandaloneWSResponse;
import play.libs.ws.ahc.StandaloneAhcWSClient;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we'll need to add this to many classes. Not sure it is worth it shouldn't be hard to tell when it is missing and you need it.

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