Skip to content

Instantly share code, notes, and snippets.

@Frontrider
Last active August 3, 2018 16:10
Show Gist options
  • Save Frontrider/93c3a4fece9903698345f76fad57c85c to your computer and use it in GitHub Desktop.
Save Frontrider/93c3a4fece9903698345f76fad57c85c to your computer and use it in GitHub Desktop.
apply plugin: 'java'
apply plugin: 'com.example.testplugin'
buildscript {
repositories {
mavenCentral()
dependencies {
}
}
}
plugins {
id 'groovy'
id 'java'
id 'java-gradle-plugin'
id 'idea'
}
repositories {
mavenCentral()
}
group 'com.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
sourceSets {
main {
java { srcDirs = [] } // no source dirs for the java compiler
groovy { srcDirs = ["src/main/java", "src/main/groovy"] } // compile everything in src/ with groovy
}
functionalTest {
groovy {
srcDir file('src/functionalTest/groovy')
outputDir = file('out/functionalTest')
resources{
file("src/functionalTest/resources")
}
}
java {
srcDir file('src/functionalTest/java')
outputDir = file('out/functionalTest')
resources{
file("src/functionalTest/resources")
}
}
resources {
srcDir file('src/functionalTest/resources')
}
compileClasspath += sourceSets.main.output + configurations.testRuntime
runtimeClasspath += output + compileClasspath
}
}
dependencies {
compile "org.codehaus.groovy:groovy-all:$groovy_version"
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4j_version
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4j_version
testCompile(
'org.mockito:mockito-all:1.10.19',
"org.junit.platform:junit-platform-runner:$junit_platform_version",
"org.junit.jupiter:junit-jupiter-params:$junit_version",
'org.spockframework:spock-unitils:1.1-groovy-2.4',
"net.bytebuddy:byte-buddy:1.8.0",
"org.objenesis:objenesis:2.6"
)
testCompile group: 'org.junit-pioneer', name: 'junit-pioneer', version: junit_pioneer_version
testCompileOnly(
'junit:junit:4.12'
)
testImplementation(
"org.junit.jupiter:junit-jupiter-api:$junit_version"
)
testRuntimeOnly(
"org.junit.vintage:junit-vintage-engine:$junit_version",
"org.junit.jupiter:junit-jupiter-engine:$junit_version",
"org.junit.platform:junit-platform-launcher:$junit_platform_version"
)
functionalTestCompile(
gradleTestKit(),
sourceSets.main.output,
'org.junit-pioneer:junit-pioneer:0.1.2',
'org.mockito:mockito-all:1.10.19',
"org.junit.platform:junit-platform-runner:$junit_platform_version",
"org.junit.jupiter:junit-jupiter-params:$junit_version",
)
functionalTestImplementation(
"org.junit.jupiter:junit-jupiter-api:$junit_version"
)
functionalTestRuntimeOnly(
"org.junit.jupiter:junit-jupiter-engine:$junit_version",
"org.junit.platform:junit-platform-launcher:$junit_platform_version",
files("./build/pluginUnderTestMetadata/plugin-under-test-metadata.properties")
)
}
gradlePlugin {
plugins {
testPlugin {
id = 'com.example.testplugin'
implementationClass = 'com.example.testplugin.TestPlugin'
}
}
testSourceSets sourceSets.functionalTest
}
task functionalTest(type: Test) {
group = "verification"
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
outputs.upToDateWhen { false }
useJUnitPlatform()
}
idea {
module {
testSourceDirs += project.sourceSets.functionalTest.java.srcDirs
testSourceDirs += project.sourceSets.functionalTest.groovy.srcDirs
testSourceDirs += project.sourceSets.functionalTest.resources.srcDirs
}
}
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import java.io.*;
import java.nio.file.Path;
/**
* @author Kis András Gábor
* 2018.08.03.
*/
@DisplayName("Testing the plugin")
public class TestRun {
@Test
@Tag("functional")
@ExtendWith(TempDirectory.class)
@DisplayName("the plugin can be applied and configured")
void test(@TempDirectory.TempDir Path directory) throws IOException {
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResource("Build.gradle").openStream()));
final String buildScript = bufferedReader.lines().reduce("", (accumulator, current) -> accumulator + current);
bufferedReader.close();
final File buildFile = new File(directory + "/build.gradle");
final FileWriter buildFileWriter = new FileWriter(buildFile);
buildFileWriter.write(buildScript);
buildFileWriter.close();
final BuildResult result = GradleRunner.create()
.withProjectDir(directory.toFile())
.withPluginClasspath()
.withGradleVersion("4.6")
.build();
System.out.println("result = " + result.getOutput());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment