- Notifications
You must be signed in to change notification settings - Fork 327
Junit5 test with dependencies runner #2962
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
JonasKunz merged 8 commits into elastic:main from JonasKunz:junit5-test-with-dependencies-runner Jan 16, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
2bf7ee8 Added capability to run JUnit5 test with different dependencies
JonasKunz bb3db1f Moved runners into testutils, switched names
JonasKunz 2171c74 Merge remote-tracking branch 'elastic/main' into junit5-test-with-dep…
JonasKunz 5a08c64 add exemple migration for httpclient3
SylvainJuge a5ae7e7 Added comment to clarify annotation use case
JonasKunz ce6124d Merge remote-tracking branch 'elastic/main' into junit5-test-with-dep…
JonasKunz 898b7ea Switched to BOM for JUnit
JonasKunz 4a8b938 Disabled failing webflux tests
JonasKunz 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
67 changes: 67 additions & 0 deletions 67 ...ore/src/test/java/co/elastic/apm/agent/testutils/JUnit4TestClassWithDependencyRunner.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,67 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. 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 co.elastic.apm.agent.testutils; | ||
| | ||
| import org.junit.runner.JUnitCore; | ||
| import org.junit.runner.Result; | ||
| import org.junit.runner.notification.Failure; | ||
| import org.junit.runners.BlockJUnit4ClassRunner; | ||
| | ||
| import javax.annotation.Nullable; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| | ||
| /** | ||
| * @deprecated use {@link TestClassWithDependencyRunner} and test with junit5 instead | ||
| */ | ||
| @Deprecated | ||
| public class JUnit4TestClassWithDependencyRunner extends AbstractTestClassWithDependencyRunner { | ||
| | ||
| @Nullable | ||
| private final BlockJUnit4ClassRunner testRunner; | ||
| | ||
| public JUnit4TestClassWithDependencyRunner(String groupId, String artifactId, String version, Class<?> testClass, Class<?>... classesReferencingDependency) throws Exception { | ||
| this(Collections.singletonList(groupId + ":" + artifactId + ":" + version), testClass, classesReferencingDependency); | ||
| } | ||
| | ||
| public JUnit4TestClassWithDependencyRunner(List<String> dependencies, Class<?> testClass, Class<?>... classesReferencingDependency) throws Exception { | ||
| this(dependencies, testClass.getName(), Arrays.stream(classesReferencingDependency).map(Class::getName).toArray(String[]::new)); | ||
| } | ||
| | ||
| public JUnit4TestClassWithDependencyRunner(List<String> dependencies, String testClass, String... classesReferencingDependency) throws Exception { | ||
| super(dependencies, testClass, classesReferencingDependency); | ||
| testRunner = new BlockJUnit4ClassRunner(this.testClass); | ||
| } | ||
| | ||
| public void run() { | ||
| if (testRunner == null) { | ||
| throw new IllegalStateException(); | ||
| } | ||
| Result result = new JUnitCore().run(testRunner); | ||
| for (Failure failure : result.getFailures()) { | ||
| System.out.println(failure); | ||
| failure.getException().printStackTrace(); | ||
| } | ||
| assertThat(result.wasSuccessful()).isTrue(); | ||
| } | ||
| | ||
| } |
104 changes: 104 additions & 0 deletions 104 ...gent-core/src/test/java/co/elastic/apm/agent/testutils/TestClassWithDependencyRunner.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,104 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. 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 co.elastic.apm.agent.testutils; | ||
| | ||
| import org.junit.jupiter.api.extension.ConditionEvaluationResult; | ||
| import org.junit.jupiter.api.extension.ExecutionCondition; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.platform.launcher.Launcher; | ||
| import org.junit.platform.launcher.LauncherDiscoveryRequest; | ||
| import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; | ||
| import org.junit.platform.launcher.core.LauncherFactory; | ||
| import org.junit.platform.launcher.listeners.SummaryGeneratingListener; | ||
| import org.junit.platform.launcher.listeners.TestExecutionSummary; | ||
| | ||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import java.lang.reflect.AnnotatedElement; | ||
| import java.util.List; | ||
| | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled; | ||
| import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled; | ||
| import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation; | ||
| import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | ||
| | ||
| public class TestClassWithDependencyRunner extends AbstractTestClassWithDependencyRunner { | ||
| | ||
| | ||
| /** | ||
| * Prevents test from running when the test class is not executed from within a {@link TestClassWithDependencyRunner}. | ||
| * <p> | ||
| * This if for example useful when attempting to test that an instrumentation is *not* active for unsupported versions | ||
| * of the instrumentation target. To test this, one would create a test that checks that the instrumentation is not active | ||
| * and run it via the {@link TestClassWithDependencyRunner} for the unsupported versions of the instrumentation target. | ||
| * <p> | ||
| * However, the test itself would also be run by maven outside the {@link TestClassWithDependencyRunner}, because it is a | ||
| * normal unit test. In this environment the test is executed with the latest version of the instrumentation target (from the pom.xml), | ||
| * which in turn would cause the test to fail because this version is actually supported. | ||
| * To prevent these "wrong" failures, this annotation can be used to disable the test outside the {@link TestClassWithDependencyRunner}. | ||
| */ | ||
| @Target({ElementType.TYPE, ElementType.METHOD}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @ExtendWith(DisableOutsideOfRunnerCondition.class) | ||
| public @interface DisableOutsideOfRunner { | ||
| /** | ||
| * @return a custom reason for disabling this outside of the dependency runner | ||
| */ | ||
| String value() default ""; | ||
| } | ||
| | ||
| public static class DisableOutsideOfRunnerCondition implements ExecutionCondition { | ||
| | ||
| @Override | ||
| public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { | ||
| AnnotatedElement element = context.getElement().orElse(null); | ||
| return findAnnotation(element, DisableOutsideOfRunner.class) | ||
| .map(annotation -> disabled(element + " is @DisableOutsideDependencyRunner", annotation.value())) | ||
| .orElse(enabled("@DisableOutsideDependencyRunner is not present")); | ||
| } | ||
| } | ||
| | ||
| public TestClassWithDependencyRunner(List<String> dependencies, String testClass, String... classesReferencingDependency) throws Exception { | ||
| super(dependencies, testClass, classesReferencingDependency); | ||
| } | ||
| | ||
| public void run() { | ||
| LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() | ||
| .selectors(selectClass(testClass)) | ||
| .configurationParameter("junit.jupiter.conditions.deactivate", DisableOutsideOfRunnerCondition.class.getName()) | ||
| .build(); | ||
| Launcher launcher = LauncherFactory.create(); | ||
| SummaryGeneratingListener listener = new SummaryGeneratingListener(); | ||
| launcher.registerTestExecutionListeners(listener); | ||
| launcher.execute(request); | ||
| | ||
| for (TestExecutionSummary.Failure failure : listener.getSummary().getFailures()) { | ||
| System.out.println(failure); | ||
| failure.getException().printStackTrace(); | ||
| } | ||
| assertThat(listener.getSummary().getTestsFailedCount()).isZero(); | ||
| } | ||
| | ||
| } | ||
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
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
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.