An open API service indexing awesome lists of open source software.

https://github.com/joke/spock-outputcapture

OutputCapture for the Spock Framework
https://github.com/joke/spock-outputcapture

output-checking spock-extension spock-framework

Last synced: 5 months ago
JSON representation

OutputCapture for the Spock Framework

Awesome Lists containing this project

README

          

= `spock-outputcapture`

:icons: font

image:https://github.com/joke/spock-deepmock/workflows/build/badge.svg[]
image:https://img.shields.io/github/license/joke/spring-factory[GitHub]
image:https://img.shields.io/maven-central/v/io.github.joke/spock-outputcapture?label=latest%20version[link=https://search.maven.org/artifact/io.github.joke/spock-outputcapture]
image:https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg[link=https://conventionalcommits.org]
image:https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit[pre-commit, link=https://github.com/pre-commit/pre-commit]

`spock-outputcapture` captures `System.out` and `System.err` in order to be verified during tests written with the http://spockframework.org/[Spock Framework].

It is intended as groovier replacement for https://spring.io/projects/spring-boot[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
image:https://img.shields.io/maven-central/v/io.github.joke/spock-outputcapture?label=latest%20version[link=https://search.maven.org/artifact/io.github.joke/spock-outputcapture]

.build.gradle
[source,groovy]
----
dependencies {
testImplementation 'io.github.joke:spock-outputcapture:x.y.z'
}
----

.Maven pom.xml
[source,xml]
----


io.github.joke
spock-outputcapture
x.y.z
test

----

== Usage

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

.Basic setup
[source,groovy]
----
// 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
[source,groovy]
----
@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.
[source,groovy]
----
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 link:examples[].

== Contributing

If you're looking to contribute, you can find additional information in link:CONTRIBUTING.adoc[here].