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 @Rule3 final TemporaryFolder testProjectDir = new TemporaryFolder()45 File buildFile6 File settingsFile78 def setup() {9 buildFile = testProjectDir.newFile('build.gradle')10 buildFile << """11 // put your default build.gradle for your tests here12 """1314 settingsFile = testProjectDir.newFile('settings.gradle')15 settingsFile << """16 rootProject.name = "myplugin"17 """18 }1920 def "my-task-added-by-my-plugin runs without failing"() {21 given:22 buildFile << """23 // I want to do something different24 plugins {25 id 'org.tipper.myplugin'26 }27 // etc28 """2930 when:31 def result = GradleRunner.create()32 .withProjectDir(testProjectDir.root)33 .withArguments('my-task-added-by-my-plugin')34 .withPluginClasspath()35 .build()3637 then:38 result.task(":my-task-added-by-my-plugin").outcome == SUCCESS39 }40}
Further details can be found in the Gradle docs.