Verify no exceptions were thrown in Spock
Asked Answered
M

1

43

I am brand new to Spock and perused their online docs. I have a test case where I need to verify that my fixture's interaction with a non-mock collaborator does not produce an exception:

class FizzSpec extends Specification {
    def "no exception thrown when we hail buzz"() {
        given:
        Fizz fixture = new Fizz()
        Buzz buzz = new Buzz("YES", true, "Garble barb") // A non-mock!

        when:
        fixture.hail(buzz)

        // TODO: How to verify the hail didn't produce an exception?
        // then:
        // thrown() == null
    }
}

Any ideas as to how I can accomplish this?

Mideast answered 20/8, 2015 at 19:41 Comment(1)
If you know the exception type, you can do notThrown(NullPointerException)Berard
B
90

Found it.

You can use

noExceptionThrown()

To assert nothing was thrown

Berard answered 20/8, 2015 at 20:22 Comment(2)
Here is the API definition spockframework.github.io/spock/javadoc/1.0/spock/lang/…Merissameristem
Do you write it after/before the statement that throws the exception? Or do you pass the statement as an argument to noExceptionThrown()?Unbounded

© 2022 - 2024 — McMap. All rights reserved.