groovy - Throw IOException in multiple return results with Spock -
i'm testing error handling. want first call mockedobject.foo()
throw new ioexception, , second return bar. tried following code,
mockedobject.foo() >>> [{throw new ioexception()}, bar]
but, when run test, error stating closure cannot cast bar,
foospec$_$spock_feature_0_1_closure2 cannot cast bar
how can mock behavior spock?
edit: after seeing documentation referenced tim_yates, changed test to,
mockedobject.foo() >>> firstbar >> {throw new ioexception()} >> secondbar
this comes close enough testing needed test. following code threw same exception, i'm guessing spock setting return type of mocked method based on first object return.
mockedobject.foo() >>> {throw new ioexception()} >> secondbar
here, complete working example:
@grab('org.spockframework:spock-core:0.7-groovy-2.0') @grab('cglib:cglib:3.1') @grab('org.ow2.asm:asm-all:5.0.3') import spock.lang.* class mytestspec extends specification { def 'spec'() { given: def = new a() a.b = mock(b) { foob() >> { throw new exception('aaa') } >> 1 } when: a.fooa() then: def e = thrown(exception) e.message == 'aaa' when: def r = a.fooa() then: r == 1 } } class { b b = new b() integer fooa() { b.foob() } } class b { integer foob() { 2 } }
Comments
Post a Comment