Groovy and Spock tests

Gradle runs on Groovy, so a Groovy project needs no plugin from a repository: the groovy plugin compiles the sources with the Groovy that Gradle already carries. Spock is the specification framework of that language, and it is a dependency like any other, which is where this image stops.

The groovy plugin

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'groovy' } dependencies { implementation localGroovy() } tasks.register('greet', JavaExec) { group = 'shop' description = 'Runs the Groovy script src/main/groovy/greet.groovy' classpath = sourceSets.main.runtimeClasspath mainClass = 'greet' }

localGroovy() is the Groovy of the Gradle installation, 4.0.33 in this image. The plugin adds src/main/groovy and src/test/groovy as source folders and the tasks compileGroovy and compileTestGroovy, and a script of the source folder compiles to a class named after its file, which is why mainClass = 'greet' works.

A Groovy script in the build

src/main/groovy/greet.groovy

def names = ['Gradle', 'Groovy'] names.each { name -> println "Hello, ${name}" }

gradle greet --console=plain --offline --no-daemon

> Task :compileJava NO-SOURCE
> Task :compileGroovy
> Task :processResources NO-SOURCE
> Task :classes

> Task :greet
Hello, Gradle
Hello, Groovy

BUILD SUCCESSFUL in 25s
2 actionable tasks: 2 executed

The task is a JavaExec of the runtime class path, so the Groovy runtime is on it and the script runs as a program. A project that runs more than one script writes this block once and passes the script name as an argument.

Spock, which this image cannot resolve

Spock is a test framework for Groovy, built on the JUnit Platform, and a Gradle build declares it like any other dependency:

build.gradle

plugins { id 'groovy' } dependencies { implementation localGroovy() testImplementation 'org.spockframework:spock-core:2.4-groovy-5.0' } tasks.named('test') { useJUnitPlatform() }

In this image that build does not reach the tests: Spock is not in the local Maven repository and there is no network. The command is marked as not run, and the two jars it would need are not named as if they had been resolved.

gradle test --console=plain --offline --no-daemon

The specification below is checked all the same. The repository carries the Spock 2.4 jar and its four dependencies in /opt/spock, and the checker of these lessons compiles the classes of the page with them and runs the specification. The last line of that run is:

python3 container/tools/course_check/code_blocks.py --lang spock container/src/ru/dvps/gradle/039_groovy_and_spock.php

The specification

src/main/groovy/Namer.groovy

final class Namer { static String slug(String text) { text.trim().toLowerCase().replaceAll(/\s+/, '-') } }

src/test/groovy/NamerSpec.groovy

import spock.lang.Specification class NamerSpec extends Specification { def "a name becomes a slug"() { expect: Namer.slug(raw) == slug where: raw | slug ' Gradle Course' | 'gradle-course' 'build' | 'build' } }

Three blocks of the specification replace a loop of assertions: expect: holds the condition, where: the table of inputs, and every row is one run of the feature method. A failure names the row that failed, which is the reason a Spock specification reads better than a loop.

Related

Автор статьи: Arthur Isaev