chennu2020 opened a new pull request, #36254:
URL: https://github.com/apache/beam/pull/36254
### 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
```java
// 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
```java
// 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](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more
tips on [how to make review process
smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
To check the build health, please visit
[https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more
information about GitHub Actions CI or the [workflows
README](https://github.com/apache/beam/blob/master/.github/workflows/README.md)
to see a list of phrases to trigger workflows.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]