Skip to content
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

Correctly configure Junit5 for non-default test sets #701

Merged
merged 4 commits into from
Jul 22, 2019
Merged
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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanna move the log.info line from 54 to just above here, then we can also include which task name we're enabling it for?

});
});
});
}

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just double checked and this should keep the junit-jupiter-engine as runtime only. https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter/5.5.1

}
'''.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()
}
}