41

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?

smeeb
  • 27,777
  • 57
  • 250
  • 447

1 Answers1

84

Found it.

You can use

noExceptionThrown()

To assert nothing was thrown

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • 1
    Here is the API definition http://spockframework.github.io/spock/javadoc/1.0/spock/lang/Specification.html#noExceptionThrown-- – aloksahoo Apr 06 '16 at 16:00
  • Do you write it after/before the statement that throws the exception? Or do you pass the statement as an argument to `noExceptionThrown()`? – Klesun Nov 04 '21 at 16:29