A few short specs showing what Spockk actually adds on top of plain Kotlin tests.
Block descriptions turn a feature into a story anyone can read, engineer or not.
class BridgeOperationsSpec : Specification() {
fun `raising shields under attack`() {
given("the Enterprise cruising at impulse")
val enterprise = Enterprise()
`when`("a Klingon bird-of-prey decloaks nearby")
enterprise.raiseShields()
then("shields are at full strength")
enterprise.shieldStrength == 100
}
}A bare condition is enough: no assertion framework required, and a failure renders a rich value diagram.
fun `shields hold against a single torpedo hit`() {
given
val enterprise = Enterprise(shieldStrength = 100)
`when`
enterprise.absorbHit(30)
then
enterprise.shieldStrength == 70
}Condition not satisfied:
enterprise.shieldStrength == 70
| | |
| 40 false
Enterprise(shieldStrength=40)A data table declares feature iterations, exactly like Spock does in Groovy.
fun `warp factor #factor is within safety limits`(
factor: Int,
safe: Boolean
) {
expect
enterprise.isWarpSafe(factor) == safe
where
factor ; safe
1 ; true
6 ; true
10 ; false
}No annotations needed: Spockk recognizes them by name, same as Spock does in Groovy.
class BridgeSystemsSpec : Specification() {
fun setupSpec() {
enterprise.initializeDiagnostics()
}
fun `bridge and engineering report ready`() {
`when`
enterprise.runDiagnostics("bridge", "engineering")
then
enterprise.diagnostics.size == 2
cleanup
enterprise.resetDiagnostics()
}
}Verify a collaborator was called, and stub its response - Spock's mocking syntax, natively in Kotlin.
fun `raising shields draws power from the deflector grid`() {
given
val powerGrid = Mock(PowerGrid::class.java)
val enterprise = Enterprise(powerGrid)
`when`
enterprise.raiseShields()
then
1 * powerGrid.divertPower(200)
}Spock's own extensions work in Spockk too.
class WarpCoreSpec : Specification() {
@FailsWith(WarpCoreBreachException::class)
fun `exceeding maximum warp causes a core breach`() {
expect
enterprise.engageWarp(10)
}
}