-
Notifications
You must be signed in to change notification settings - Fork 4.5k
JUnit5 support #35688
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
Merged
Merged
JUnit5 support #35688
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a45e9e9
JUnit 5 support.
ce30d2d
Update JUnit BOM version to 5.13.4
dc94d4e
Update CHANGES.md with JUnit 5 support for 2.67.0
f351c9f
Update CHANGES.md with correct issue reference #18733
9ceae75
Fix JMS module tests for JUnit 5 compatibility
c9fac8d
Enhance JUnit 5 support with improved TestPipelineExtension
915b788
Conservative JUnit 5 implementation with minimal dependencies
18dcfbe
Java: Move JUnit TestPipelineExtension to sdks/java/testing/junit; re…
be470fa
Move JUnit4to5MigrationExampleTest to testing:junit; remove from core…
110ad55
Merge branch 'master' into junit5-support
chennu2020 c44c5a1
test: stabilize flaky tests for CI
fc273a0
docs: update CHANGES.md for JUnit 5 support; core: update TestPipelin…
4f21846
Merge branch 'master' into junit5-support
chennu2020 8ac2d9f
Update CHANGES.md
chennu2020 6c99215
Update CHANGES.md - move JUnit5 support to 2.68.0
chennu2020 bd75731
Update CHANGES.md
chennu2020 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
227 changes: 227 additions & 0 deletions
227
sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipelineExtension.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,227 @@ | ||
| /* | ||
| * 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.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState; | ||
|
|
||
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import org.apache.beam.sdk.options.ApplicationNameOptions; | ||
| import org.apache.beam.sdk.options.PipelineOptions; | ||
| import org.apache.beam.sdk.testing.TestPipeline.PipelineAbandonedNodeEnforcement; | ||
| import org.apache.beam.sdk.testing.TestPipeline.PipelineRunEnforcement; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.junit.jupiter.api.extension.AfterEachCallback; | ||
| import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.jupiter.api.extension.ParameterContext; | ||
| import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
|
||
| /** | ||
| * JUnit 5 extension for {@link TestPipeline} that provides the same functionality as the JUnit 4 | ||
| * {@link org.junit.rules.TestRule} implementation. | ||
| * | ||
| * <p>Use this extension to test pipelines in JUnit 5: | ||
| * | ||
| * <pre><code> | ||
| * {@literal @}ExtendWith(TestPipelineExtension.class) | ||
| * class MyPipelineTest { | ||
| * {@literal @}Test | ||
| * {@literal @}Category(NeedsRunner.class) | ||
| * void myPipelineTest(TestPipeline pipeline) { | ||
| * final PCollection<String> pCollection = pipeline.apply(...) | ||
| * PAssert.that(pCollection).containsInAnyOrder(...); | ||
| * pipeline.run(); | ||
| * } | ||
| * } | ||
| * </code></pre> | ||
| * | ||
| * <p>You can also create the extension yourself for more control: | ||
| * | ||
| * <pre><code> | ||
| * class MyPipelineTest { | ||
| * {@literal @}RegisterExtension | ||
| * final TestPipelineExtension pipeline = TestPipelineExtension.create(); | ||
| * | ||
| * {@literal @}Test | ||
| * void testUsingPipeline() { | ||
| * pipeline.apply(...); | ||
| * pipeline.run(); | ||
| * } | ||
| * } | ||
| * </code></pre> | ||
| */ | ||
| public class TestPipelineExtension | ||
| implements BeforeEachCallback, AfterEachCallback, ParameterResolver { | ||
|
|
||
| private static final ExtensionContext.Namespace NAMESPACE = | ||
| ExtensionContext.Namespace.create(TestPipelineExtension.class); | ||
| private static final String PIPELINE_KEY = "testPipeline"; | ||
| private static final String ENFORCEMENT_KEY = "enforcement"; | ||
|
|
||
| /** Creates a new TestPipelineExtension with default options. */ | ||
| public static TestPipelineExtension create() { | ||
| return new TestPipelineExtension(); | ||
| } | ||
|
|
||
| /** Creates a new TestPipelineExtension with custom options. */ | ||
| public static TestPipelineExtension fromOptions(PipelineOptions options) { | ||
| return new TestPipelineExtension(options); | ||
| } | ||
|
|
||
| private TestPipeline testPipeline; | ||
|
|
||
| /** Creates a TestPipelineExtension with default options. */ | ||
| public TestPipelineExtension() { | ||
| this.testPipeline = TestPipeline.create(); | ||
| } | ||
|
|
||
| /** Creates a TestPipelineExtension with custom options. */ | ||
| public TestPipelineExtension(PipelineOptions options) { | ||
| this.testPipeline = TestPipeline.fromOptions(options); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportsParameter( | ||
| ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
| return parameterContext.getParameter().getType() == TestPipeline.class; | ||
| } | ||
|
|
||
| @Override | ||
| public Object resolveParameter( | ||
| ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
| if (this.testPipeline == null) { | ||
| return getOrCreateTestPipeline(extensionContext); | ||
| } else { | ||
| return this.testPipeline; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void beforeEach(ExtensionContext context) throws Exception { | ||
| TestPipeline pipeline; | ||
|
|
||
| if (this.testPipeline != null) { | ||
| pipeline = this.testPipeline; | ||
| } else { | ||
| pipeline = getOrCreateTestPipeline(context); | ||
| } | ||
|
|
||
| Optional<PipelineRunEnforcement> enforcement = getOrCreateEnforcement(context); | ||
|
|
||
| // Set application name based on test method | ||
| String appName = getAppName(context); | ||
| pipeline.getOptions().as(ApplicationNameOptions.class).setAppName(appName); | ||
|
|
||
| // Set up enforcement based on annotations | ||
| setDeducedEnforcementLevel(context, pipeline, enforcement); | ||
| } | ||
|
|
||
| @Override | ||
| public void afterEach(ExtensionContext context) throws Exception { | ||
| Optional<PipelineRunEnforcement> enforcement = getEnforcement(context); | ||
| if (enforcement.isPresent()) { | ||
| enforcement.get().afterUserCodeFinished(); | ||
| } | ||
| } | ||
|
|
||
| private TestPipeline getOrCreateTestPipeline(ExtensionContext context) { | ||
| return context | ||
| .getStore(NAMESPACE) | ||
| .getOrComputeIfAbsent(PIPELINE_KEY, key -> TestPipeline.create(), TestPipeline.class); | ||
| } | ||
|
|
||
| private Optional<PipelineRunEnforcement> getOrCreateEnforcement(ExtensionContext context) { | ||
| return context | ||
| .getStore(NAMESPACE) | ||
| .getOrComputeIfAbsent( | ||
| ENFORCEMENT_KEY, key -> Optional.<PipelineRunEnforcement>empty(), Optional.class); | ||
| } | ||
|
|
||
| private Optional<PipelineRunEnforcement> getEnforcement(ExtensionContext context) { | ||
| return context.getStore(NAMESPACE).get(ENFORCEMENT_KEY, Optional.class); | ||
| } | ||
|
|
||
| private void setEnforcement( | ||
| ExtensionContext context, Optional<PipelineRunEnforcement> enforcement) { | ||
| context.getStore(NAMESPACE).put(ENFORCEMENT_KEY, enforcement); | ||
| } | ||
chennu2020 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private String getAppName(ExtensionContext context) { | ||
| String className = context.getTestClass().map(Class::getSimpleName).orElse("UnknownClass"); | ||
| String methodName = context.getTestMethod().map(Method::getName).orElse("unknownMethod"); | ||
| return className + "-" + methodName; | ||
| } | ||
|
|
||
| private void setDeducedEnforcementLevel( | ||
| ExtensionContext context, | ||
| TestPipeline pipeline, | ||
| Optional<PipelineRunEnforcement> enforcement) { | ||
| // If enforcement level has not been set, do auto-inference | ||
| if (!enforcement.isPresent()) { | ||
| boolean annotatedWithNeedsRunner = hasNeedsRunnerAnnotation(context); | ||
|
|
||
| PipelineOptions options = pipeline.getOptions(); | ||
| boolean crashingRunner = CrashingRunner.class.isAssignableFrom(options.getRunner()); | ||
|
|
||
| checkState( | ||
| !(annotatedWithNeedsRunner && crashingRunner), | ||
| "The test was annotated with a [@%s] / [@%s] while the runner " | ||
| + "was set to [%s]. Please re-check your configuration.", | ||
| NeedsRunner.class.getSimpleName(), | ||
| ValidatesRunner.class.getSimpleName(), | ||
| CrashingRunner.class.getSimpleName()); | ||
|
|
||
| if (annotatedWithNeedsRunner || !crashingRunner) { | ||
| Optional<PipelineRunEnforcement> newEnforcement = | ||
| Optional.of(new PipelineAbandonedNodeEnforcement(pipeline)); | ||
| setEnforcement(context, newEnforcement); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean hasNeedsRunnerAnnotation(ExtensionContext context) { | ||
| // Check method annotations | ||
| Method testMethod = context.getTestMethod().orElse(null); | ||
| if (testMethod != null) { | ||
| if (hasNeedsRunnerCategory(testMethod.getAnnotations())) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Check class annotations | ||
| Class<?> testClass = context.getTestClass().orElse(null); | ||
| if (testClass != null) { | ||
| if (hasNeedsRunnerCategory(testClass.getAnnotations())) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private boolean hasNeedsRunnerCategory(Annotation[] annotations) { | ||
| return Arrays.stream(annotations) | ||
| .filter(annotation -> annotation instanceof Category) | ||
| .map(annotation -> (Category) annotation) | ||
| .flatMap(category -> Arrays.stream(category.value())) | ||
| .anyMatch(categoryClass -> NeedsRunner.class.isAssignableFrom(categoryClass)); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.