Skip to content

Skip JUnit 5 TIA tests before constructing the test class #11178

Closed
jbolsens-legion wants to merge 2 commits intoDataDog:masterfrom
jbolsens-legion:jb/fix-junit5-tia-skip-class-construction
Closed

Skip JUnit 5 TIA tests before constructing the test class #11178
jbolsens-legion wants to merge 2 commits intoDataDog:masterfrom
jbolsens-legion:jb/fix-junit5-tia-skip-class-construction

Conversation

@jbolsens-legion
Copy link
Copy Markdown

What Does This Do

Moves the JUnit 5 TIA/ITR skip decision from Node.shouldBeSkipped to the earlier NodeTestTask.execute() hook, so that test classes for skipped methods are never constructed. Adds a regression test that asserts the skipped test class's constructor is not invoked.

Motivation

When Mockito.mockStatic(...) is used as a field-level initializer, allowing the class to be constructed before deciding to skip registers a static mock that is never closed (because @AfterEach doesn't run for skipped tests). The next test's field initializer then throws "static mocking is already registered in the current thread." Same pattern breaks any class-level resource whose teardown lives in @AfterEach.

@daniel-mohedano
Copy link
Copy Markdown
Contributor

daniel-mohedano commented Apr 22, 2026

Hey @jbolsens-legion, thanks a lot for taking the time to contribute! As mentioned in the support ticket, my hesitation with the approach is that we would be moving away from JUnit's extension point of Node.shouldBeSkipped, also used by other framework features such as @Disabled or ExecutionCondition, and introducing the skip earlier in the life cycle. This is why the issue still reproduces without any tracer injection when using any of those skipping methods. Although the listener API is used correctly here with your changes, it could introduce unintended side effects on systems depending on the regular sequence that I'd like to test more thoroughly on my side.

How viable would it be on your side to move the mockStatic (or any other resources that might cause the exception) into @BeforeEach? That would keep the behavior identical with and without the agent, and is also recommended by the JUnit team in this case (junit-team/junit-framework#1358)

@jbolsens-legion
Copy link
Copy Markdown
Author

@daniel-mohedano thanks for finding that on junit side, I was not aware that this pattern is the norm.

If this is the same pattern, I agree probably this PR should be closed, I will work to resolve issues in our own tests.

@jbolsens-legion jbolsens-legion deleted the jb/fix-junit5-tia-skip-class-construction branch April 22, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tag: community Community contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants