Skip to content

Conversation

@chennu2020
Copy link
Contributor

Issue

When using Apache Beam's TestPipelineExtension with JUnit 5, tests would fail with:

java.lang.IllegalStateException: Is your TestPipeline declaration missing a @Rule annotation? 
Usage: @Rule public final transient TestPipeline pipeline = TestPipeline.create();

Root Cause Analysis

  1. TestPipeline Internal Validation: The TestPipeline class had internal validation logic that expected the @Rule annotation to be present, even when used in JUnit 5 context through TestPipelineExtension.

  2. Enforcement Coordination Issue: The TestPipelineExtension was creating duplicate enforcement mechanisms that conflicted with TestPipeline's internal enforcement, leading to coordination problems.

  3. JUnit 5 Context Detection: TestPipeline lacked proper detection of JUnit 5 execution context, causing it to apply JUnit 4-specific validation rules inappropriately.

Solution Implementation

1. Enhanced TestPipeline.java

File: sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipeline.java

Key Changes:

  • JUnit 5 Context Detection: Added logic to detect JUnit 5 execution context by inspecting the call stack for JUnit 5-specific classes
  • Conditional Enforcement: Modified run() method to set default enforcement when running in JUnit 5 context
  • Backward Compatibility: Maintained full compatibility with existing JUnit 4 usage patterns
// Enhanced run() method with JUnit 5 detection
@Override
public PipelineResult run() {
  // Detect JUnit 5 context and set enforcement if needed
  if (enforcement.get() == null && isJUnit5Context()) {
    setDeducedEnforcementLevel();
  }
  return super.run();
}

private boolean isJUnit5Context() {
  // Stack trace inspection to detect JUnit 5 execution
  return Arrays.stream(Thread.currentThread().getStackTrace())
      .anyMatch(frame -> frame.getClassName().startsWith("org.junit.jupiter"));
}

2. Improved TestPipelineExtension.java

File: sdks/java/testing/junit/src/main/java/org/apache/beam/sdk/testing/TestPipelineExtension.java

Key Changes:

  • Enforcement Initialization: Enabled enforcement in constructors with enableAbandonedNodeEnforcement(true)
  • Removed Duplicate Enforcement: Eliminated duplicate enforcement creation in setDeducedEnforcementLevel() to prevent conflicts
  • Simplified Coordination: Let TestPipeline handle its own enforcement mechanism instead of creating separate extension enforcement
// Simplified enforcement coordination
private void setDeducedEnforcementLevel() {
  // Let TestPipeline handle enforcement - avoid duplication
  if (testPipeline.enforcement.get() == null) {
    testPipeline.setDeducedEnforcementLevel();
  }
}

3. Comprehensive Test Coverage

Files:

  • TestPipelineJUnit5CompatibilityTest.java - New comprehensive JUnit 5 compatibility tests
  • TestPipelineJUnit4And5InteroperabilityTest.java - Interoperability validation tests

Test Coverage:

  • JUnit 5 pipeline injection and execution
  • Enforcement coordination between TestPipeline and TestPipelineExtension
  • Pipeline execution tracking and validation
  • Empty pipeline handling
  • Multiple pipeline operations
  • Pipeline options integration
  • PAssert integration
  • Error handling and propagation
  • JUnit 4/5 interoperability

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

- Fix TestPipeline to detect JUnit 5 context and set default enforcement
- Fix TestPipelineExtension to avoid duplicate enforcement creation
- Add comprehensive JUnit 5 compatibility and interoperability tests
- Update CHANGELOG.md with bugfix entry

Resolves IllegalStateException about missing @rule annotation when using
TestPipelineExtension with JUnit 5. Maintains full backward compatibility
with existing JUnit 4 usage patterns.

Fixes: TestPipeline.run() now works seamlessly with JUnit 5 TestPipelineExtension
@github-actions github-actions bot added the java label Sep 23, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @chennu2020, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a long-standing compatibility issue preventing Apache Beam's "TestPipeline" from working seamlessly with JUnit 5's "TestPipelineExtension". The core problem stemmed from "TestPipeline"'s internal validation expecting JUnit 4's "@rule" annotation even in a JUnit 5 context. The solution involves enhancing "TestPipeline" to intelligently detect its execution environment and adapt its enforcement logic, while also refining "TestPipelineExtension" to better coordinate with "TestPipeline"'s internal mechanisms. This ensures that developers can now use "TestPipeline" with JUnit 5 without encountering "IllegalStateException"s, maintaining full backward compatibility with JUnit 4, and significantly improving the testing experience for Beam users.

Highlights

  • JUnit 5 Compatibility: Resolves a critical "IllegalStateException" in "TestPipeline.run()" when used with JUnit 5's "TestPipelineExtension", which previously incorrectly demanded a "@rule" annotation.
  • Conditional Enforcement in TestPipeline: "TestPipeline.java" now includes logic to detect JUnit 5 execution contexts by inspecting the call stack, allowing it to conditionally apply enforcement mechanisms and bypass JUnit 4-specific "@rule" validation.
  • Streamlined TestPipelineExtension: "TestPipelineExtension.java" has been updated to initialize enforcement directly in its constructors and "getOrCreateTestPipeline" method, and to eliminate redundant enforcement creation, preventing conflicts and simplifying coordination with "TestPipeline".
  • Enhanced Test Coverage: New comprehensive test files, "TestPipelineJUnit5CompatibilityTest.java" and "TestPipelineJUnit4And5InteroperabilityTest.java", have been added to validate JUnit 5 compatibility, ensure backward compatibility with JUnit 4, and confirm proper enforcement coordination and pipeline execution tracking.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@liferoad liferoad requested review from Abacn and Amar3tto September 23, 2025 22:21
@github-actions
Copy link
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

* TestPipelineExtension. This is used to provide JUnit 5 compatibility without breaking JUnit 4
* behavior.
*/
private boolean isJUnit5Context() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like an ad-hoc fix. There should be a cleaner way to do this (than bump stack trace and check class name). Let me also do some experiments.

Copy link
Contributor

Choose a reason for hiding this comment

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

opened #36258

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you

@chennu2020 chennu2020 closed this Sep 24, 2025
@chennu2020 chennu2020 deleted the junit5-testpipeline-compatibility-fix branch September 24, 2025 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants