Skip to content

joke/spock-outputcapture

Repository files navigation

spock-outputcapture

badge GitHub spock outputcapture?label=latest%20version Conventional%20Commits 1.0.0 yellow pre-commit

spock-outputcapture captures System.out and System.err in order to be verified during tests written with the Spock Framework.

It is intended as groovier replacement for Spring Boot OutputCaptureRule, which has been superseed by the OutputCaptureExtension that on the other hand is not working with Spock.

  • Groovy syntax

  • Captures both System.out and System.err

  • Captures logs of the entire Spec via

    • @Share @OutputCapture globalLogs

  • Captures logs per Test

    • @OutputCapture localLogs

  • Working with Spock Framework 2.0, 2.1, 2.2, 2.3 and 2.4-M2

Dependency setup

Gradle build.gradle

spock outputcapture?label=latest%20version

build.gradle
dependencies {
    testImplementation 'io.github.joke:spock-outputcapture:x.y.z'
}
Maven pom.xml
<dependencies>
  <dependency>
    <groupId>io.github.joke</groupId>
    <artifactId>spock-outputcapture</artifactId>
    <version>x.y.z</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Usage

Add the output capture to your specification. globalLogs and localLogs will contain logged messages.

Basic setup
// Contains all logs of the entire specification
@Shared @OutputCapture globalLogs

// contains logs the of current feature method only
@OutputCapture localLogs

If you need more control over the logs object you can add the type.

With concrete type
@Shared @OutputCapture CapturedOutput globalLogs
@OutputCapture CapturedOutput localLogs

In most cases you want to perform basic regex matching on the output.

Full example of how to use verify the output.
class LoggingSpec extends Specification {

    @OutputCapture localLogs

    def 'local log contains service name'() {
        setup:
        new LoggingService().logYourName()

        expect:
        localLogs ==~ /(?s).*My name is LoggingService.*/
    }
}

Take a look at the examples.

Contributing

If you’re looking to contribute, you can find additional information in here.