Skip to content

Avoid Adding Duplicated JUnit Entries on Classpath #712

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions common/junit-platform-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ maven {
}
dependencies {
compileOnly libs.graalvm.svm
implementation(platform(libs.test.junit.bom))
implementation libs.test.junit.platform.console
implementation libs.test.junit.platform.launcher
implementation libs.test.junit.jupiter.core
compileOnly(platform(libs.test.junit.bom))
compileOnly libs.test.junit.platform.console
compileOnly libs.test.junit.platform.launcher
compileOnly libs.test.junit.jupiter.core
testImplementation libs.test.junit.vintage

testRuntimeOnly(platform(libs.test.junit.bom))
testRuntimeOnly libs.test.junit.platform.console
testRuntimeOnly libs.test.junit.platform.launcher
testRuntimeOnly libs.test.junit.jupiter.core
}

apply from: "gradle/native-image-testing.gradle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ private static void initializeClassesForJDK21OrEarlier() {
try (InputStream is = JUnitPlatformFeature.class.getResourceAsStream("/initialize-at-buildtime")) {
if (is != null) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
br.lines().forEach(RuntimeClassInitialization::initializeAtBuildTime);
br.lines().forEach(cls -> {
try {
RuntimeClassInitialization.initializeAtBuildTime(cls);
} catch (NoClassDefFoundError e) {
// if users use older JUnit versions some classes might not be available
}
});
}
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ public static void main(String... args) {
out.println("JUnit Platform on Native Image - report");
out.println("----------------------------------------\n");
out.flush();
launcher.registerTestExecutionListeners(new PrintTestExecutionListener(out));
configurePrintTestExecutionListener(launcher, out);
}

SummaryGeneratingListener summaryListener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(summaryListener);
launcher.registerTestExecutionListeners(new LegacyXmlReportGeneratingListener(Paths.get(xmlOutput), out));
configureLegacyXMLReport(launcher, xmlOutput, out);
launcher.execute(testPlan);

TestExecutionSummary summary = summaryListener.getSummary();
Expand Down Expand Up @@ -271,4 +271,21 @@ private static boolean testIdsDirectoryExists(Path directory) {
return directory != null && Files.exists(directory);
}


private static void configurePrintTestExecutionListener(Launcher launcher, PrintWriter out) {
try {
Class.forName("org.junit.platform.reporting.legacy.LegacyReportingUtils");
launcher.registerTestExecutionListeners(new PrintTestExecutionListener(out));
} catch (NoClassDefFoundError | ClassNotFoundException e) {
// intentionally ignored
}
}

private static void configureLegacyXMLReport(Launcher launcher, String xmlOutput, PrintWriter out) {
try {
launcher.registerTestExecutionListeners(new LegacyXmlReportGeneratingListener(Paths.get(xmlOutput), out));
} catch (NoClassDefFoundError e) {
// intentionally ignored
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.FieldSource;
import org.junit.jupiter.params.provider.MethodSource;

import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -86,9 +86,15 @@ public void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration r
AnnotationUtils.forEachAnnotatedMethodParameter(testClass, AggregateWith.class, annotation -> registry.registerAllClassMembersForReflection(annotation.value()));
AnnotationUtils.forEachAnnotatedMethod(testClass, EnumSource.class, (m, annotation) -> handleEnumSource(m, annotation, registry));
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, MethodSource.class, JupiterConfigProvider::handleMethodSource);
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, FieldSource.class, JupiterConfigProvider::handleFieldSource);
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, EnabledIf.class, JupiterConfigProvider::handleEnabledIf);
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, DisabledIf.class, JupiterConfigProvider::handleDisabledIf);

try {
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, FieldSource.class, JupiterConfigProvider::handleFieldSource);
} catch (NoClassDefFoundError e) {
// if users use JUnit version older than 5.11, FieldSource class won't be available
}

}

private static Class<?>[] handleMethodSource(MethodSource annotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ org.junit.jupiter.engine.discovery.MethodSelectorResolver$MethodType$3
org.junit.jupiter.engine.discovery.predicates.IsTestClassWithTests
org.junit.jupiter.engine.discovery.predicates.IsTestFactoryMethod
org.junit.jupiter.engine.execution.ConditionEvaluator
org.junit.jupiter.engine.execution.ExecutableInvoker
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall
org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall$VoidMethodInterceptorCall
Expand Down Expand Up @@ -78,6 +79,7 @@ org.junit.platform.launcher.core.DiscoveryIssueNotifier
org.junit.platform.launcher.core.EngineDiscoveryOrchestrator
org.junit.platform.launcher.core.EngineExecutionOrchestrator
org.junit.platform.launcher.core.EngineFilterer
org.junit.platform.launcher.core.EngineIdValidator
org.junit.platform.launcher.core.HierarchicalOutputDirectoryProvider
org.junit.platform.launcher.core.InternalTestPlan
org.junit.platform.launcher.core.LauncherConfig
Expand All @@ -90,6 +92,8 @@ org.junit.platform.launcher.core.LauncherDiscoveryResult
org.junit.platform.launcher.core.LauncherDiscoveryResult$EngineResultInfo
org.junit.platform.launcher.core.LauncherListenerRegistry
org.junit.platform.launcher.core.ListenerRegistry
org.junit.platform.launcher.core.ServiceLoaderRegistry
org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry
org.junit.platform.launcher.core.SessionPerRequestLauncher
org.junit.platform.launcher.EngineDiscoveryResult
org.junit.platform.launcher.LauncherSessionListener$1
Expand Down
6 changes: 6 additions & 0 deletions native-maven-plugin/reproducers/issue-144/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.resolution.DependencyResult;
import org.graalvm.buildtools.utils.FileUtils;
import org.graalvm.buildtools.utils.JUnitUtils;
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;

Expand All @@ -76,7 +75,6 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down
2 changes: 2 additions & 0 deletions samples/java-application-with-custom-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ dependencies {
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
testImplementation('org.junit.jupiter:junit-jupiter')
integTestImplementation project(":")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
testRuntimeOnly("org.junit.platform:junit-platform-reporting:1.11.0")
}

def integTest = tasks.register("integTest", Test) {
Expand Down
1 change: 1 addition & 0 deletions samples/java-application-with-reflection/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def junitVersion = providers.gradleProperty('junit.jupiter.version')
dependencies {
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
}

test {
Expand Down
6 changes: 6 additions & 0 deletions samples/java-application-with-reflection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
1 change: 1 addition & 0 deletions samples/java-application-with-resources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def junitVersion = providers.gradleProperty('junit.jupiter.version')
dependencies {
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
}

test {
Expand Down
6 changes: 6 additions & 0 deletions samples/java-application-with-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
2 changes: 2 additions & 0 deletions samples/java-application-with-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def junitVersion = providers.gradleProperty('junit.jupiter.version')
dependencies {
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
testRuntimeOnly("org.junit.platform:junit-platform-reporting:1.11.0")
}

test {
Expand Down
6 changes: 6 additions & 0 deletions samples/java-application-with-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
1 change: 1 addition & 0 deletions samples/java-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def junitVersion = providers.gradleProperty('junit.jupiter.version')
dependencies {
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
}

graalvmNative {
Expand Down
6 changes: 6 additions & 0 deletions samples/java-library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
1 change: 1 addition & 0 deletions samples/junit-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.0'
testImplementation "org.junit.vintage:junit-vintage-engine:5.11.0"
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
}

application {
Expand Down
6 changes: 6 additions & 0 deletions samples/junit-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand Down
1 change: 1 addition & 0 deletions samples/kotlin-application-with-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

testImplementation 'org.jetbrains.kotlin:kotlin-test'
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
}

application {
Expand Down
1 change: 1 addition & 0 deletions samples/multi-project-with-tests/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
implementation(project(":utils"))
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.0")
}

test {
Expand Down
6 changes: 6 additions & 0 deletions samples/multi-project-with-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading