Skip to content

Commit

Permalink
Correctly configure Junit5 for non-default test sets (#701)
Browse files Browse the repository at this point in the history
Determine whether to use junitPlatform on a per source set basis
  • Loading branch information
ferozco authored and bulldozer-bot[bot] committed Jul 22, 2019
1 parent 37d2b89 commit eb96da0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 25 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-701.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Determine whether to use junitPlatform on a per source set basis
links:
- https://github.com/palantir/gradle-baseline/pull/701
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import java.util.Objects;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.testing.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,38 +43,61 @@ public void apply(Project project) {
}
});

project.getPlugins().withType(JavaPlugin.class, p -> {

project.getPlugins().withType(JavaPlugin.class, unused -> {
// afterEvaluate necessary because the junit-jupiter dep might be added further down the build.gradle
project.afterEvaluate(unused -> {
project.getConfigurations()
.getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME)
.getAllDependencies()
.matching(dep -> Objects.equals(dep.getGroup(), "org.junit.jupiter")
&& dep.getName().equals("junit-jupiter"))
.stream()
.findAny()
.ifPresent(ignored -> enableJUnit5ForAllTestTasks(project));
project.afterEvaluate(proj -> {
proj.getConvention()
.getPlugin(JavaPluginConvention.class)
.getSourceSets()
.matching(ss -> hasCompileDependenciesMatching(proj, ss, this::isJunitJupiter))
.forEach(ss -> {
String testTaskName = ss.getTaskName(null, "test");
Test testTask = (Test) proj.getTasks().findByName(testTaskName);
if (testTask == null) {
// Fall back to the source set name, since that is what gradle-testsets-plugin does
testTask = (Test) proj.getTasks().findByName(ss.getName());
if (testTask == null) {
log.warn(
"Detected 'org:junit.jupiter:junit-jupiter', but unable to find test task");
return;
}
}
log.info("Detected 'org:junit.jupiter:junit-jupiter', enabling useJUnitPlatform() on {}",
testTask.getName());
enableJunit5ForTestTask(testTask);
});
});
});
}

private void enableJUnit5ForAllTestTasks(Project project) {
log.info("Detected 'org:junit.jupiter:junit-jupiter', enabling useJUnitPlatform()");
project.getTasks().withType(Test.class).configureEach(task -> {
task.useJUnitPlatform();
private boolean hasCompileDependenciesMatching(Project project, SourceSet sourceSet, Spec<Dependency> spec) {
return project.getConfigurations()
.getByName(sourceSet.getCompileClasspathConfigurationName())
.getAllDependencies()
.matching(spec)
.stream()
.findAny()
.isPresent();
}

task.systemProperty("junit.platform.output.capture.stdout", "true");
task.systemProperty("junit.platform.output.capture.stderr", "true");
private boolean isJunitJupiter(Dependency dep) {
return Objects.equals(dep.getGroup(), "org.junit.jupiter")
&& dep.getName().equals("junit-jupiter");
}

// https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution
task.systemProperty("junit.jupiter.execution.parallel.enabled", "true");
private void enableJunit5ForTestTask(Test task) {
task.useJUnitPlatform();

// Computes the desired parallelism based on the number of available processors/cores
task.systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic");
task.systemProperty("junit.platform.output.capture.stdout", "true");
task.systemProperty("junit.platform.output.capture.stderr", "true");

// provide some stdout feedback when tests fail
task.testLogging(testLogging -> testLogging.events("failed"));
});
// https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution
task.systemProperty("junit.jupiter.execution.parallel.enabled", "true");

// Computes the desired parallelism based on the number of available processors/cores
task.systemProperty("junit.jupiter.execution.parallel.config.strategy", "dynamic");

// provide some stdout feedback when tests fail
task.testLogging(testLogging -> testLogging.events("failed"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.palantir.baseline


import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome

Expand Down Expand Up @@ -79,4 +78,30 @@ class BaselineTestingIntegrationTest extends AbstractPluginTest {
new File(projectDir, "build/reports/tests/test/classes/test.TestClass4.html").exists()
new File(projectDir, "build/reports/tests/test/classes/test.TestClass5.html").exists()
}

def 'runs integration tests with junit5'() {
when:
buildFile << '''
plugins {
id 'org.unbroken-dome.test-sets' version '2.1.1'
}
'''.stripIndent()
buildFile << standardBuildFile
buildFile << '''
testSets {
integrationTest
}
dependencies {
integrationTestImplementation "org.junit.jupiter:junit-jupiter:5.4.2"
}
'''.stripIndent()
file('src/integrationTest/java/test/TestClass5.java') << junit5Test

then:
BuildResult result = with('integrationTest', '--write-locks').build()
result.task(':integrationTest').outcome == TaskOutcome.SUCCESS
new File(projectDir, "build/reports/tests/integrationTest/classes/test.TestClass5.html").exists()
}
}

0 comments on commit eb96da0

Please sign in to comment.