Skip to content

Commit

Permalink
Merge pull request gradle#9118 from epeee/init_junit_jupiter
Browse files Browse the repository at this point in the history
Build Init plugin: add possibility to use JUnit Jupiter for Java proj…
  • Loading branch information
adammurdoch authored Apr 16, 2019
2 parents 2a8f6fe + 3958073 commit 479e6bd
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ include("child")
--test-framework Set the test framework to be used.
Available values are:
junit
junitjupiter
kotlintest
scalatest
spock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ class JavaApplicationInitIntegrationTest extends AbstractInitIntegrationSpec {
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}

@Unroll
def "creates sample source using junitjupiter instead of junit with #scriptDsl build scripts"() {
when:
run('init', '--type', 'java-application', '--test-framework', 'junitjupiter', '--dsl', scriptDsl.id)

then:
targetDir.file("src/main/java").assertHasDescendants(SAMPLE_APP_CLASS)
targetDir.file("src/test/java").assertHasDescendants(SAMPLE_APP_TEST_CLASS)

and:
commonJvmFilesGenerated(scriptDsl)

when:
run("build")

then:
assertTestPassed("some.thing.AppTest", "appHasAGreeting")

where:
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}

@Unroll
def "creates sample source with package and #testFramework and #scriptDsl build scripts"() {
when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ class JavaLibraryInitIntegrationTest extends AbstractInitIntegrationSpec {
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}

@Unroll
def "creates sample source using junitjupiter instead of junit with #scriptDsl build scripts"() {
when:
run('init', '--type', 'java-library', '--test-framework', 'junitjupiter', '--dsl', scriptDsl.id)

then:
targetDir.file("src/main/java").assertHasDescendants(SAMPLE_LIBRARY_CLASS)
targetDir.file("src/test/java").assertHasDescendants(SAMPLE_LIBRARY_TEST_CLASS)

and:
commonJvmFilesGenerated(scriptDsl)
def dslFixture = dslFixtureFor(scriptDsl)
buildFileSeparatesImplementationAndApi(dslFixture, 'org.junit.jupiter')

when:
run("build")

then:
assertTestPassed("some.thing.LibraryTest", "testSomeLibraryMethod")

where:
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}

@Unroll
def "creates sample source with package and #testFramework and #scriptDsl build scripts"() {
when:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ protected TemplateOperation testTemplateOperation(InitSettings settings) {
return fromClazzTemplate("javaapp/testng/AppTest.java.template", settings, "test", "java");
case JUNIT:
return fromClazzTemplate("javaapp/AppTest.java.template", settings, "test");
case JUNITJUPITER:
return fromClazzTemplate("javaapp/junitjupiter/AppTest.java.template", settings, "test");
default:
throw new IllegalArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protected TemplateOperation testTemplateOperation(InitSettings settings) {
return fromClazzTemplate("javalibrary/testng/LibraryTest.java.template", settings, "test");
case JUNIT:
return fromClazzTemplate("javalibrary/LibraryTest.java.template", settings, "test");
case JUNITJUPITER:
return fromClazzTemplate("javalibrary/junitjupiter/LibraryTest.java.template", settings, "test");
default:
throw new IllegalArgumentException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework;

import java.util.Arrays;
Expand All @@ -27,6 +28,7 @@
import static org.gradle.api.plugins.JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME;
import static org.gradle.api.plugins.JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME;
import static org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework.JUNIT;
import static org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework.JUNITJUPITER;
import static org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework.SPOCK;
import static org.gradle.buildinit.plugins.internal.modifiers.BuildInitTestFramework.TESTNG;

Expand Down Expand Up @@ -99,6 +101,20 @@ private void addTestFramework(BuildInitTestFramework testFramework, BuildScriptB
"Use TestNG for unit tests",
"test", "Test", "useTestNG");
break;
case JUNITJUPITER:
buildScriptBuilder
.dependency(
getTestImplementationConfigurationName(),
"Use JUnit Jupiter API for testing.",
"org.junit.jupiter:junit-jupiter-api:" + libraryVersionProvider.getVersion("junitjupiter")
).dependency(JavaPlugin.TEST_RUNTIME_ONLY_CONFIGURATION_NAME,
"Use JUnit Jupiter Engine for testing.",
"org.junit.jupiter:junit-jupiter-engine:" + libraryVersionProvider.getVersion("junitjupiter")
).taskMethodInvocation(
"Use junit platform for unit tests",
"test", "Test", "useJUnitPlatform"
);
break;
default:
buildScriptBuilder
.dependency(getTestImplementationConfigurationName(), "Use JUnit test framework", "junit:junit:" + libraryVersionProvider.getVersion("junit"));
Expand All @@ -124,7 +140,7 @@ public BuildInitTestFramework getDefaultTestFramework() {

@Override
public Set<BuildInitTestFramework> getTestFrameworks() {
return new TreeSet<BuildInitTestFramework>(Arrays.asList(JUNIT, TESTNG, SPOCK));
return new TreeSet<BuildInitTestFramework>(Arrays.asList(JUNIT, JUNITJUPITER, TESTNG, SPOCK));
}

protected static class Description {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public enum BuildInitTestFramework {
TESTNG,
SPOCK,
KOTLINTEST,
SCALATEST;
SCALATEST,
JUNITJUPITER;

public static List<String> listSupported() {
List<String> result = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
${packageDecl.javaStatement}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
${packageDecl.javaStatement}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class LibraryTest {
@Test void testSomeLibraryMethod() {
Library classUnderTest = new Library();
assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ commons-math=3.6.1
groovy=2.5.6
guava=27.1-jre
junit=4.12
junitjupiter=5.4.2
kotlin=1.3.21
scala-library=2.12.8
scala-xml=1.1.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class BuildInitTestFrameworkTest extends Specification {
def result = BuildInitTestFramework.listSupported();

then:
result.size() == 5
result.size() == 6
result[0] == "junit"
result[1] == "testng"
result[2] == "spock"
result[3] == "kotlintest"
result[4] == "scalatest"
result[5] == "junitjupiter"
}
}

0 comments on commit 479e6bd

Please sign in to comment.