TE

Groovy Testing

Groovy testing conventions with Spock Framework

Details

Language / Topic
groovyGroovy
Category
Testing

Rules

balanced
- Write tests with Spock Framework — extend `spock.lang.Specification` and structure tests in `given:`, `when:`, `then:` blocks.
- Use `expect:` blocks for pure data transformation tests and `where:` blocks for data-driven parameterized tests.
- Use Spock mocks (`Mock()`, `Stub()`, `Spy()`) for dependency isolation and verify interactions with `1 * service.method(arg)`.
- Use `@Unroll` on data-driven tests and define meaningful iteration descriptions with `"#input produces #expected"` in the method name.
- Use `thrown(ExceptionType)` in the `then:` block to assert that a specific exception is thrown by the `when:` block.
- Use `setupSpec()` for expensive one-time setup shared across all tests in a spec and `setup()` for per-test initialization.
- Use Spock's `@Shared` annotation for fields that are expensive to initialize and safe to share across test instances.