Interaction Based Testing: how does Spock know about asynchronous calls?

1,544 views
Skip to first unread message

Fedor Belov

unread,
Sep 17, 2014, 8:48:20 AM9/17/14
to spockfr...@googlegroups.com
I've got following test:

setup:
MyService myMock = Mock()
anotherService.myService = myMock

when:
anotherService.doA(false)

then:
0 * myMock._

when:
anotherService.doA(true)

then:
1 *  myMock._

 doA asynchronously (with 2 seconds delay) calls myService. How does Spock checks asynchronous call (I checked it - if myService will be called in first case than test will fail)?

Peter Niederwieser

unread,
Sep 19, 2014, 2:53:49 AM9/19/14
to spockfr...@googlegroups.com
Spock can't possibly know that the calls happen asynchronously. It's the test's responsibility to not leave the `when` block until all asynchronous code triggered from within the block has finished.

Witold Sowa

unread,
Nov 19, 2014, 4:54:58 PM11/19/14
to spockfr...@googlegroups.com
Peter, do you know any better way than sleep(x) at the end of 'when' block to achieve it? Let's stick to Fedor's example as it is very clear.

pnie...@gmail.com

unread,
Nov 19, 2014, 5:52:41 PM11/19/14
to spockfr...@googlegroups.com
You could use a `spock.util.concurrent.BlockingVariable` that is set in the mock response, and read in a condition in the then-block.

Cheers,
Peter

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

Roberto Guerra

unread,
Dec 30, 2014, 9:26:41 AM12/30/14
to spockfr...@googlegroups.com
This is probably a little late.
I blogged about testing async here http://blog.stumblingoncode.com/posts/2014-11-06-ratpack-guava-eventbus.html 

Leonard Brünings

unread,
Jan 19, 2015, 11:58:58 AM1/19/15
to spockfr...@googlegroups.com
Another option would be to use polling conditions, see here http://docs.spockframework.org/en/latest/new_and_noteworthy.html
Reply all
Reply to author
Forward
0 new messages