John Tipper
Navigate back to the homepage

Gradle Plugin Development – Functional Testing with Spock

John Tipper
July 18th, 2018 · 1 min read

Spock is a beautifully expressive testing framework. Personally, I find it results in perhaps a 1/3 less code than JUnit and the tests are to my eye much easier to understand.

Here’s an example of a functional test (see the earlier blog post for details) of how to use Spock and GradleRunner to perform functional tests.

1class MyPluginFunctionalSpec extends Specification {
2 @Rule
3 final TemporaryFolder testProjectDir = new TemporaryFolder()
4
5 File buildFile
6 File settingsFile
7
8 def setup() {
9 buildFile = testProjectDir.newFile('build.gradle')
10 buildFile << """
11 // put your default build.gradle for your tests here
12 """
13
14 settingsFile = testProjectDir.newFile('settings.gradle')
15 settingsFile << """
16 rootProject.name = "myplugin"
17 """
18 }
19
20 def "my-task-added-by-my-plugin runs without failing"() {
21 given:
22 buildFile << """
23 // I want to do something different
24 plugins {
25 id 'org.tipper.myplugin'
26 }
27 // etc
28 """
29
30 when:
31 def result = GradleRunner.create()
32 .withProjectDir(testProjectDir.root)
33 .withArguments('my-task-added-by-my-plugin')
34 .withPluginClasspath()
35 .build()
36
37 then:
38 result.task(":my-task-added-by-my-plugin").outcome == SUCCESS
39 }
40}

Further details can be found in the Gradle docs.

More articles from John Tipper

Gradle for large projects – customising multi-project build files

Customising Gradle multi-project builds by renaming the build.gradle file to be named after the project itself.

July 18th, 2018 · 1 min read

Gradle – Integration Testing for Gradle Plugin Development

How to get started with Integration Testing for Gradle plugin development.

July 18th, 2018 · 1 min read
© 2018–2021 John Tipper
Link to $https://twitter.com/john_tipperLink to $https://github.com/john-tipperLink to $https://www.linkedin.com/in/john-tipper-5076395