Mastering Spock: Practical Unit Test Examples in Groovy
This article introduces the versatile Spock testing framework, explains why it’s favored for Groovy projects despite compatibility quirks, and provides a collection of runnable demo specs that showcase Spock’s expressive syntax, data‑driven testing, and detailed reporting.
Spock is a powerful, all‑in‑one unit‑testing framework that combines expressive specifications with rich reporting.
During a recent evaluation of unit‑testing tools—including JUnit, Mockito, and PowerMock—the author selected Spock for its Groovy integration, despite some compatibility issues with advanced Mockito syntax. The framework excels in test case readability, report generation, and clear error messages.
Demo Specification
The following Groovy spec demonstrates common Spock features such as shared fixtures, data‑driven tests, and the when‑then structure.
package com.FunTester.spock.pratice
import org.slf4j.Logger
import spock.lang.Shared
import spock.lang.Specification
import static com.fun.frame.SourceCode.getLogger
class test01 extends Specification {
@Shared int a = 10
@Shared Logger logger = getLogger(this.getClass().getName())
def setupSpec() {
logger.info "Testing class started! ${logger.getName()}"
}
def setup() {
logger.info "Testing method started!"
}
def "simple equality test"() {
given: "prepare test data"
def s = 3
def ss = 3
expect: "verify result"
s == ss
}
def "data‑driven expression test"() {
given: "prepare data"
expect: "test method"
z == x + y
where:
x | y || z
1 | 0 || 1
2 | 1 || 3
}
def "object method test"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Kirk" | 4
"Scotty" | 6
"Sc3otty" | 7
}
def "array‑based data test"() {
expect:
name.size() == length
where:
name << ["fjdslj", "fds"]
length << [6, 3]
}
def "object verification"() {
given:
def per = new Per("fun", 12)
expect:
with(per) {
name == "fun"
age == 12
}
}
def "when‑then structure test"() {
when:
def s = plus(3, 2)
def ss = plus(3, 2)
then:
verifyAll {
s == 3
ss == 3
}
}
/**
* Simple addition method used in the when‑then test
*/
def plus(int i, int j) {
i + j
}
/**
* Helper class for object verification
*/
class Per {
Per(String name, int age) {
this.name = name
this.age = age
}
String name
int age
}
}Test Report Screenshots
The generated Spock reports provide a clear overview of each specification, including passed/failed assertions and detailed stack traces. The following images illustrate the default HTML report layout.
These screenshots demonstrate how Spock’s built‑in reporting makes test outcomes instantly understandable, which is especially useful for teams adopting behavior‑driven development.
Overall, the article offers a hands‑on guide for developers who want to experiment with Spock, covering setup, common patterns, and visual feedback through the framework’s reporting features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
