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

Fix gradle - junit5 integration templates and instructions #4600

Merged
merged 4 commits into from
Oct 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private class BasicRestProject {
private ProjectWriter writer;
private String srcMainPath;
private String testMainPath;
private String nativeTestMainPath;
private SourceType type;

private BasicRestProject(final ProjectWriter writer, final Map<String, Object> parameters) {
Expand All @@ -76,13 +77,19 @@ private boolean initProject() throws IOException {

srcMainPath = writer.mkdirs(type.getSrcDir());
testMainPath = writer.mkdirs(type.getTestSrcDir());
// for gradle we want to place the native tests in under 'src/native-test/java'
// since gradle's idiomatic way of running integration tests is to create a new source set
if (getBuildTool() == BuildTool.GRADLE) {
nativeTestMainPath = writer.mkdirs(type.getTestSrcDir().replace("test", "native-test"));
} else {
nativeTestMainPath = testMainPath;
}

return newProject;
}

private boolean initBuildTool() throws IOException {
BuildFile buildFileManager = get(BUILD_FILE, null);
BuildTool buildTool = buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool();
BuildTool buildTool = getBuildTool();
context.putIfAbsent(ADDITIONAL_GITIGNORE_ENTRIES, buildTool.getGitIgnoreEntries());
boolean newProject = !writer.exists(buildTool.getDependenciesFile());
if (newProject) {
Expand Down Expand Up @@ -110,6 +117,11 @@ private boolean initBuildTool() throws IOException {
return newProject;
}

private BuildTool getBuildTool() {
BuildFile buildFileManager = get(BUILD_FILE, null);
return buildFileManager == null ? BuildTool.MAVEN : buildFileManager.getBuildTool();
}

private void generate(final String templateName, final Map<String, Object> context, final String outputFilePath,
final String resourceType)
throws IOException {
Expand Down Expand Up @@ -176,9 +188,18 @@ private void setupContext() throws IOException {

if (packageName != null) {
String packageDir = srcMainPath + '/' + packageName.replace('.', '/');
String originalTestMainPath = testMainPath;
String testPackageDir = testMainPath + '/' + packageName.replace('.', '/');
srcMainPath = writer.mkdirs(packageDir);
testMainPath = writer.mkdirs(testPackageDir);

if (!originalTestMainPath.equals(nativeTestMainPath)) {
String nativeTestPackageDir = nativeTestMainPath + '/' + packageName.replace('.', '/');
nativeTestMainPath = writer.mkdirs(nativeTestPackageDir);
} else {
nativeTestMainPath = testMainPath;
}

} else {
throw new NullPointerException("Need a non-null package name");
}
Expand All @@ -192,7 +213,7 @@ private void createClasses() throws IOException {
String extension = type.getExtension();
String classFile = srcMainPath + '/' + className + extension;
String testClassFile = testMainPath + '/' + className + "Test" + extension;
String itTestClassFile = testMainPath + '/' + "Native" + className + "IT" + extension;
String itTestClassFile = nativeTestMainPath + '/' + "Native" + className + "IT" + extension;
String name = getName();
String srcResourceTemplate = type.getSrcResourceTemplate(name);
Object isSpring = context.get(IS_SPRING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ dependencies {

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'

nativeTestImplementation 'io.quarkus:quarkus-junit5'
nativeTestImplementation 'io.rest-assured:rest-assured'
}

group '${project_groupId}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ dependencies {

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'

nativeTestImplementation 'io.quarkus:quarkus-junit5'
nativeTestImplementation 'io.rest-assured:rest-assured'
}

group '${project_groupId}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ dependencies {

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'

nativeTestImplementation 'io.quarkus:quarkus-junit5'
nativeTestImplementation 'io.rest-assured:rest-assured'
}

group '${project_groupId}'
version '${project_version}'

41 changes: 38 additions & 3 deletions devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package io.quarkus.gradle;

import java.util.Collections;

import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.SourceSetOutput;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.testing.Test;
import org.gradle.util.GradleVersion;
Expand All @@ -17,6 +24,7 @@
import io.quarkus.gradle.tasks.QuarkusListExtensions;
import io.quarkus.gradle.tasks.QuarkusNative;
import io.quarkus.gradle.tasks.QuarkusTestConfig;
import io.quarkus.gradle.tasks.QuarkusTestNative;

/**
* @author <a href="mailto:[email protected]">Ståle Pedersen</a>
Expand Down Expand Up @@ -52,11 +60,38 @@ private void registerTasks(Project project) {
quarkusBuild.dependsOn(classesTask);
});

tasks.create("buildNative", QuarkusNative.class).dependsOn(quarkusBuild);
Task buildNative = tasks.create("buildNative", QuarkusNative.class).dependsOn(quarkusBuild);

// set up the source set for the testNative
JavaPluginConvention javaPlugin = project.getConvention().findPlugin(JavaPluginConvention.class);
if (javaPlugin != null) {
SourceSetContainer sourceSets = javaPlugin.getSourceSets();
SourceSet nativeTestSourceSet = sourceSets.create("native-test"); // this name has to be the same as the directory in which the tests reside
SourceSetOutput mainSourceSetOutput = sourceSets.getByName("main").getOutput();
SourceSetOutput testSourceSetOutput = sourceSets.getByName("test").getOutput();
nativeTestSourceSet.setCompileClasspath(
nativeTestSourceSet.getCompileClasspath().plus(mainSourceSetOutput).plus(testSourceSetOutput));
nativeTestSourceSet.setRuntimeClasspath(
nativeTestSourceSet.getRuntimeClasspath().plus(mainSourceSetOutput).plus(testSourceSetOutput));

// create a custom configuration to be used for the dependencies of the testNative task
ConfigurationContainer configurations = project.getConfigurations();
configurations.maybeCreate("nativeTestImplementation").extendsFrom(configurations.findByName("implementation"));
configurations.maybeCreate("nativeTestRuntimeOnly").extendsFrom(configurations.findByName("runtimeOnly"));

Task testNative = tasks.create("testNative", QuarkusTestNative.class).dependsOn(buildNative);
testNative.setShouldRunAfter(Collections.singletonList(tasks.findByName("test")));

tasks.getByName("check").dependsOn(testNative);
}

// Quarkus test configuration task which should be executed before any Quarkus test
final QuarkusTestConfig quarkusTestConfig = tasks.create("quarkusTestConfig", QuarkusTestConfig.class);
tasks.withType(Test.class).forEach(t -> t.dependsOn(quarkusTestConfig));
tasks.withType(Test.class).forEach(t -> {
// Quarkus test configuration task which should be executed before any Quarkus test
t.dependsOn(quarkusTestConfig);
// also make each task use the JUnit platform since it's the only supported test environment
t.useJUnitPlatform();
});
}

private void verifyGradleVersion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.gradle.tasks;

import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.testing.Test;

public class QuarkusTestNative extends Test {

public QuarkusTestNative() {
setDescription("Runs native image tests");
setGroup("verification");

JavaPluginConvention javaPlugin = getProject().getConvention().getPlugin(JavaPluginConvention.class);
SourceSetContainer sourceSets = javaPlugin.getSourceSets();
SourceSet sourceSet = sourceSets.findByName("native-test");

setTestClassesDirs(sourceSet.getOutput().getClassesDirs());
setClasspath(sourceSet.getRuntimeClasspath());
}
}
22 changes: 3 additions & 19 deletions docs/src/main/asciidoc/gradle-tooling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,9 @@ dependencies {

== Enable Tests

Quarkus uses Junit5 and to enable it in Gradle we need to add a section to our build file:

[source,groovy,subs=attributes+]
----
test {
useJUnitPlatform()
}
----

Using the Kotlin DSL, add:

[source,kotlin,subs=attributes+]
----
tasks.test {
useJUnitPlatform()
}
----
When the Quarkus Gradle plugin is applied to a build, the `test` task is configured to run regular tests while the `testNative` task
is meant to run the native image tests.
The source code for the native image tests should reside in `src/native-test` instead of `src/test` where the source code for regular tests resides.

To follow up our Rest example from above, we would also need to add two test dependencies:

Expand Down Expand Up @@ -180,7 +166,6 @@ database.
[source,groovy,subs=attributes+]
----
test {
useJUnitPlatform()
systemProperty "quarkus.test.profile", "foo" <1>
}
----
Expand All @@ -190,7 +175,6 @@ or, if you use the Gradle Kotlin DSL:
[source,kotlin,subs=attributes+]
----
tasks.test {
useJUnitPlatform()
systemProperty("quarkus.test.profile", "foo") <1>
}
----
Expand Down
5 changes: 0 additions & 5 deletions docs/src/main/asciidoc/kotlin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,6 @@ tasks {
}
}

tasks.test {
useJUnitPlatform()
exclude '**/Native*'
}

allOpen { // <5>
annotation("javax.ws.rs.Path")
annotation("javax.enterprise.context.ApplicationScoped")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public final class PathTestHelper {
"bin" + File.separator + "test",
"bin" + File.separator + "main");
// gradle
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "java" + File.separator + "native-test",
"classes" + File.separator + "java" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "java" + File.separator + "test",
"classes" + File.separator + "java" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "kotlin" + File.separator + "native-test",
"classes" + File.separator + "kotlin" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "kotlin" + File.separator + "test",
"classes" + File.separator + "kotlin" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "scala" + File.separator + "native-test",
"classes" + File.separator + "scala" + File.separator + "main");
TEST_TO_MAIN_DIR_FRAGMENTS.put(
"classes" + File.separator + "scala" + File.separator + "test",
"classes" + File.separator + "scala" + File.separator + "main");
Expand Down