-
Notifications
You must be signed in to change notification settings - Fork 4.5k
This PR fixes a critical JUnit 5 compatibility issue where TestPipeline.run() would throw an IllegalStateException about missing @Rule annotation even when using the proper TestPipelineExtension for JUnit 5.
#36254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
chennu2020
wants to merge
2
commits into
apache:master
from
chennu2020:junit5-testpipeline-compatibility-fix
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...src/test/java/org/apache/beam/sdk/testing/TestPipelineJUnit4And5InteroperabilityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.sdk.testing; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| import org.apache.beam.sdk.transforms.Create; | ||
| import org.apache.beam.sdk.values.PCollection; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
|
|
||
| /** | ||
| * Test to validate that our JUnit 5 compatibility fix doesn't break JUnit 4 functionality. | ||
| * | ||
| * <p>This test ensures that: 1. TestPipeline still works correctly with JUnit 4 @Rule pattern 2. | ||
| * TestPipelineExtension works correctly with JUnit 5 3. Both approaches can coexist in the same | ||
| * codebase 4. Enforcement mechanisms work correctly for both approaches | ||
| * | ||
| * <p>This test class uses JUnit 5 but validates that the underlying TestPipeline class maintains | ||
| * backward compatibility with JUnit 4 patterns. | ||
| */ | ||
| @ExtendWith(TestPipelineExtension.class) | ||
| public class TestPipelineJUnit4And5InteroperabilityTest { | ||
|
|
||
| /** | ||
| * Test that validates TestPipeline can be created and used in the same way as it would be with | ||
| * JUnit 4 @Rule, but within a JUnit 5 context. | ||
| */ | ||
| @Test | ||
| public void testJUnit4StyleUsageInJUnit5Context(TestPipeline pipeline) { | ||
| // This mimics how TestPipeline would be used with JUnit 4 @Rule | ||
| // but ensures it works in JUnit 5 context with our fix | ||
|
|
||
| assertDoesNotThrow( | ||
| () -> { | ||
| // Create pipeline content (same as JUnit 4 usage) | ||
| PCollection<String> input = pipeline.apply("Create", Create.of("junit4-style")); | ||
| PAssert.that(input).containsInAnyOrder("junit4-style"); | ||
|
|
||
| // Run pipeline (same as JUnit 4 usage) | ||
| pipeline.run().waitUntilFinish(); | ||
| }, | ||
| "JUnit 4 style usage should work in JUnit 5 context"); | ||
| } | ||
|
|
||
| /** Test that validates enforcement behavior is consistent between JUnit 4 and JUnit 5. */ | ||
| @Test | ||
| @Category(NeedsRunner.class) | ||
| public void testEnforcementConsistencyBetweenJUnitVersions(TestPipeline pipeline) { | ||
| assertDoesNotThrow( | ||
| () -> { | ||
| // This should behave the same way in both JUnit 4 and JUnit 5 | ||
| PCollection<String> input = pipeline.apply("Create", Create.of("enforcement-test")); | ||
| PAssert.that(input).containsInAnyOrder("enforcement-test"); | ||
| pipeline.run().waitUntilFinish(); | ||
| }, | ||
| "Enforcement should work consistently across JUnit versions"); | ||
| } | ||
|
|
||
| /** | ||
| * Test that validates TestPipeline options and configuration work the same way in both JUnit 4 | ||
| * and JUnit 5 contexts. | ||
| */ | ||
| @Test | ||
| public void testPipelineOptionsConsistency(TestPipeline pipeline) { | ||
| assertNotNull(pipeline.getOptions()); | ||
|
|
||
| // Verify that pipeline options are set up the same way as in JUnit 4 | ||
| assertDoesNotThrow( | ||
| () -> { | ||
| // Application name should be set based on test context | ||
| String appName = pipeline.getOptions().getJobName(); | ||
| // This should work the same way in both JUnit versions | ||
| }, | ||
| "Pipeline options should be consistent across JUnit versions"); | ||
| } | ||
|
|
||
| /** Test that validates PAssert behavior is identical between JUnit 4 and JUnit 5. */ | ||
| @Test | ||
| @Category(NeedsRunner.class) | ||
| public void testPAssertConsistencyBetweenJUnitVersions(TestPipeline pipeline) { | ||
| assertDoesNotThrow( | ||
| () -> { | ||
| PCollection<Integer> numbers = pipeline.apply("CreateNumbers", Create.of(1, 2, 3)); | ||
|
|
||
| // PAssert should work identically in both JUnit versions | ||
| PAssert.that(numbers).containsInAnyOrder(1, 2, 3); | ||
| PAssert.thatSingleton(pipeline.apply("CreateSingle", Create.of(100))).isEqualTo(100); | ||
|
|
||
| pipeline.run().waitUntilFinish(); | ||
| }, | ||
| "PAssert should work identically in both JUnit versions"); | ||
| } | ||
|
|
||
| /** | ||
| * Test that validates our fix doesn't introduce any regressions in the core TestPipeline | ||
| * functionality. | ||
| */ | ||
| @Test | ||
| public void testNoRegressionsInCoreFunctionality(TestPipeline pipeline) { | ||
| assertDoesNotThrow( | ||
| () -> { | ||
| // Test basic pipeline operations | ||
| PCollection<String> step1 = pipeline.apply("Step1", Create.of("a", "b", "c")); | ||
| PCollection<String> step2 = pipeline.apply("Step2", Create.of("x", "y", "z")); | ||
|
|
||
| // Test assertions | ||
| PAssert.that(step1).containsInAnyOrder("a", "b", "c"); | ||
| PAssert.that(step2).containsInAnyOrder("x", "y", "z"); | ||
|
|
||
| // Test pipeline execution | ||
| pipeline.run().waitUntilFinish(); | ||
| }, | ||
| "Core TestPipeline functionality should not have regressions"); | ||
| } | ||
|
|
||
| /** Test that validates the fix works with empty pipelines in both contexts. */ | ||
| @Test | ||
| public void testEmptyPipelineHandlingConsistency(TestPipeline pipeline) { | ||
| // Empty pipelines should be handled consistently in both JUnit 4 and JUnit 5 | ||
| assertNotNull(pipeline); | ||
| assertNotNull(pipeline.getOptions()); | ||
|
|
||
| // This should not cause any enforcement issues in either JUnit version | ||
| } | ||
|
|
||
| /** Test that validates error propagation works the same way in both JUnit versions. */ | ||
| @Test | ||
| public void testErrorPropagationConsistency(TestPipeline pipeline) { | ||
| // Error handling should be consistent between JUnit 4 and JUnit 5 | ||
| assertDoesNotThrow( | ||
| () -> { | ||
| PCollection<String> input = pipeline.apply("Create", Create.of("error-propagation-test")); | ||
| PAssert.that(input).containsInAnyOrder("error-propagation-test"); | ||
| pipeline.run().waitUntilFinish(); | ||
| }, | ||
| "Error propagation should be consistent across JUnit versions"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opened #36258
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you