override mocking behaviour for one test

1,726 views
Skip to first unread message

Frederic Pape

unread,
Sep 8, 2011, 6:19:31 AM9/8/11
to Spock Framework - User
Hello,

Is it possible to override the behaviour (return value) for one
specific test method: see example below (also available at:
http://meetspock.appspot.com/script/36001)
"testing exceptional behaviour" fails where I want it to succeed

interface MyInterface {
int myMethod()
}


class MyInterfaceTest extends Specification {
def myInterface = Mock(MyInterface)

def setup() {
myInterface.myMethod() >> 1
}

def "testing regular behaviour"() {
when:
def result = myInterface.myMethod()
then:
result == 1
}

def "testing exceptional behaviour"() {
given:
myInterface.myMethod() >> 2
when:
def result = myInterface.myMethod()
then:
result == 2
}
}

Peter Niederwieser

unread,
Sep 8, 2011, 10:25:26 PM9/8/11
to Spock Framework - User
Yes. Interactions in a then-block have precedence over all other
interactions.

Cheers,
Peter

Frederic Pape

unread,
Sep 9, 2011, 3:08:42 AM9/9/11
to spockfr...@googlegroups.com
Ok nice. It works now. Thanks.

Shouldn't interactions in the given-block (of a test method) to have precedence over interactions in the setup-block (of the test class)?

Thanks,
Frederic

by the way: Peter, congrats with your excellent framework, Spock rocks :-)

Erick Dovale

unread,
Sep 9, 2011, 10:46:38 AM9/9/11
to spockfr...@googlegroups.com
As far as I know given is just an alias to setup; they are exactly the same thing.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spockframework/-/zrA2lFlEpKwJ.

To post to this group, send email to spockfr...@googlegroups.com.
To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Peter Niederwieser

unread,
Sep 11, 2011, 10:36:46 PM9/11/11
to spockfr...@googlegroups.com
I guess you are saying that interactions in given/setup blocks should have precedence over those in setup methods. We considered implementing this, but didn't deem it important enough. If you'd like to see this, feel free to submit an issue.

Cheers,
Peter

Frederic Pape

unread,
Sep 13, 2011, 7:11:45 AM9/13/11
to spockfr...@googlegroups.com
Indeed that's what I meant...
submitted an issue http://code.google.com/p/spock/issues/detail?id=199

Thanks,
Frederic
Reply all
Reply to author
Forward
0 new messages