Spock checking exception thrown by layout with layout interaction

The problem I am facing is when I try to check in the then block that an exception was thrown and that call was made.

Take a look at the installation below:

 class B { def b(A a) { aa() } } class A { def a() { } } def "foo"() { given: def a = Mock(A) aa() >> { throw new RuntimeException() } B b = new B() when: bb(a) then: thrown(RuntimeException) 1 * aa() } 

The above test failed with the message: Expected exception java.lang.RuntimeException, but no exception was thrown , but the code setting the layout explicitly throws an exception.

It's funny if you delete the last line: 1 * aa() test will pass. I did not have a similar problem when combining other statements in the then block that do not check for exceptions.

Any ideas what is going on?

+7
mocking groovy spock
source share
1 answer

It should be configured and verified as follows:

 @Grab('org.spockframework:spock-core:0.7-groovy-2.0') @Grab('cglib:cglib-nodep:3.1') import spock.lang.* class Test extends Specification { def "foo"() { given: def a = Mock(A) B b = new B() when: bb(a) then: thrown(RuntimeException) 1 * aa() >> { throw new RuntimeException() } } } class B { def b(A a) { aa() } } class A { def a() { } } 

If you are both mocking and checking out interactions, then the behavior layout should be configured in the where / then block.

+10
source share

All Articles