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.outandSystem.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
dependencies {
testImplementation 'io.github.joke:spock-outputcapture:x.y.z'
}<dependencies>
<dependency>
<groupId>io.github.joke</groupId>
<artifactId>spock-outputcapture</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
</dependencies>Add the output capture to your specification. globalLogs and localLogs will contain logged messages.
// Contains all logs of the entire specification
@Shared @OutputCapture globalLogs
// contains logs the of current feature method only
@OutputCapture localLogsIf you need more control over the logs object you can add the type.
@Shared @OutputCapture CapturedOutput globalLogs
@OutputCapture CapturedOutput localLogsIn most cases you want to perform basic regex matching on 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.
If you’re looking to contribute, you can find additional information in here.