lundi 20 novembre 2017

Spock testing output from printing function

I just wanted to know how I can test the output from a groovy function that does a println of some stuff. So the stupid class I wrote is:

class FriendlyGreeterLib {
    def greet(name) {
        println "Hi, ${name.capitalize()}"
    }
}

The corresponding test would work, if greet() returned a string. But how to check the output of println to stdout...

import spock.lang.Shared
import spock.lang.Specification

class FriendlyGreeterLibTest extends Specification{

    @Shared lib

    def setupSpec() {
        lib = new FriendlyGreeterLib()
    }

    def "FriendlyGreeterLib greets capitalized"() {
        expect:
        lib.greet(x) == y

        where:
        x | y
        'fred' | 'Fred'
        'FRED' | 'FRED'
        'fRED' | 'FRED'
    }
}

Aucun commentaire:

Enregistrer un commentaire