Spec driven spock

From edmundmiller/dotfiles · View interactive version

Spec driven spock is an AI agent skill from the GitHub repository edmundmiller/dotfiles. Uses Spock metadata to connect executable specs to design decisions. Use when authoring or refactoring Spock specifications as design documentation.

How to use this skill

Skill file

Spec-driven Spock

Spock has a small but underused set of metadata annotations that turn an

ordinary Specification into living design documentation:

| Annotation | What it pins |

| ----------------- | ------------------------------------------------------------------------ |

| @Title | One-line statement of what the spec is about (class) |

| @Narrative | Multi-line "given / so that" backstory (class) |

| @Subject | The class under test (class or field) |

| @See | URL(s) to external references — ADR, RFC, ticket, docs (class or method) |

| @Issue | URL(s) to issues / tickets specifically (class or method) |

| @PendingFeature | Marker for a not-yet-implemented behaviour — see below |

| @Unroll | Per-row report names for data-driven specs |

| @Snapshot | Inject a snapshot for snapshot-driven tests (Spock 2.4+) |

Spock renders these into HTML reports and surfaces them in IDE plugins,

which is why they pay rent. Stuffing the same information into Groovydoc

comments is invisible to the runtime.

For the per-annotation cheat sheet, read references/annotations.md.

When to reach for this skill

Pattern 1 — Class-level scaffold

Every non-trivial spec should open with this header:

@Title("One sentence — what is this spec asserting?")
@Narrative('''
Given/so-that prose. Two or three short paragraphs at most. Explain
the *audience* for this spec and what kind of change should make this
spec evolve.
''')
@See([
    "https://github.com/<org>/<repo>/blob/master/adr/YYYYMMDD-<title>.md",
    "https://github.com/<org>/<repo>/pull/<n>"
])
@Subject(ClassUnderTest)
class MyFeatureSpec extends Specification { ... }

The @See URLs render as links in Spock HTML reports and most IDE

plugins, so prefer stable URLs (a merged file path, a PR number, an

issue URL) over local paths.

Pattern 2 — Per-method `@See`

Each feature method links to _its_ design source:

@See("https://github.com/<org>/<repo>/blob/master/adr/2026-foo.md#d3--checksum-only")
def 'file outputs are compared by recorded checksum only'() {
    expect: ...
}

Use @Issue instead of @See when the link is specifically a bug or

ticket. Both accept a single String or a list of Strings, and both can

be repeated on the same target (Spock allows multiple @See annotations

on one feature).

Don't add a @See if the only reasonable link is the file the test is

already in — that's noise.

Pattern 3 — The executable contract

When an ADR / RFC lands, ship an executable contract alongside it: every

numbered decision becomes a @PendingFeature stub that runs but stays

skipped until the implementation fills it in.

@PendingFeature
@See("https://github.com/.../adr/foo.md#d7--auto-detect-ci")
def 'CI=true switches default output mode to --json'() {
    expect: false
}

Why expect: false?

This is the killer pattern for ADR-driven development. See

references/adr-contract-pattern.md

for a full worked example (a 4-file split of an 18-decision ADR), and

assets/contract-spec-template.groovy

for a copy-pasteable skeleton.

Pattern 4 — `@Unroll` for table-driven contracts

When the ADR / design pins a matrix (exit codes, format flags, status

columns), use one @Unroll spec instead of N copy-pasted methods:

@Unroll
@PendingFeature
@See("https://.../adr/foo.md#d6--exit-codes")
def 'exit code is #code when #scenario'() {
    expect: false
    where:
    code | scenario
    0    | 'runs are semantically equivalent'
    1    | 'runs differ in any failing category'
    2    | 'load failure or schema mismatch'
}

Spock renders each row as a separately-named test (exit code is 0 when runs are semantically equivalent), so the HTML report reads as if you wrote three methods, but the source stays DRY.

Pattern 5 — Splitting large contracts

If a contract spec passes ~250 lines or mixes audiences, split it.

Naming heuristic: one file per concern, one concern per audience.

For the lineage-validate ADR (18 decisions / 46 stubs), the split was:

| File | Decisions | Audience |

| -------------------------------- | ---------------------- | ------------------- |

| LineageValidateCliFlagsSpec | flags, env, exit codes | pipeline authors |

| LineageValidateEquivalenceSpec | equivalence rules | reviewers |

| LineageValidateBaselineSpec | resolver SPI, schema | plugin authors |

| LineageValidateReportingSpec | diff shape, categories | tooling integrators |

Each file gets its own @Title / @Narrative / class-level @See.

Each method keeps its own @See to the specific decision.

The split is _organisational_, not structural — the implementation

side stays a single shared core. Resist proliferating implementation

classes to match the spec split.

Pattern 6 — `@Snapshot` for snapshot-driven specs

Spock 2.4 ships first-class snapshot support. Inject a Snapshotter

into the spec, point it at a directory, assert against it:

@Snapshot
Snapshotter snapshotter

def 'renders the report as documented'() {
    expect:
    snapshotter.assertThat(actualReport).matchesSnapshot()
}

Set spock.snapshots.update=true (or run with -Dspock.snapshots.update=true) to refresh snapshots when the contract intentionally changes. Keep the snapshot files in version control next to the spec so a PR's diff shows the contract change explicitly.

Pitfalls

Workflow

When the user asks for help on a Spock spec:

  1. Read the spec. Note what the test asserts and what design decision motivates it.
  2. Find the source of truth. ADR, RFC, ticket, or PR. If there isn't one, ask whether one should exist — the spec may be the de facto source.
  3. Apply the class-level scaffold. @Title + @Narrative + @Subject + class-level @See pointing at the source.
  4. Add per-method @See only where the link is more specific than the class-level one.
  5. For ADR-driven work, draft a *ContractSpec companion with @PendingFeature stubs for every decision; split by audience if it grows past ~250 lines.
  6. Run the suite to confirm @PendingFeature stubs are skipped (not failing, not unexpectedly passing).

When inheriting a legacy *Test class that doesn't follow this style,

don't blanket-rewrite. Add annotations to the methods the current task

touches; leave the rest. Consistency across a codebase matters less than

making each spec individually self-explanatory.

Further reading

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.