Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to do mock argument capturing in Spock

Tags:

mocking

spock

I have looked around and tried different things to no avail. The examples out there on the interwebs are few, and IMHO pretty simple. My use case:

(the 'itocNetworkHandler' below is the mock)

when: "we're doing stuff"     StandardResponse response = cms.doCardStuff("123", "111", order) .... then: "we get proper calls and response object"     1 * cms.itocNetworkHandler.doNetworkCall(             { it instanceof ReplacementRequestRecord             }, StandardResponseRecord.class) >> record 

I would like to store away the parameter ('it') to the "doNetworkCall" on the mock.

The reason i want the parameter is because the object i'm testing is supposed to take my in parameters, do stuff, create a new object and pass that one to my mock. I want to make sure that the created object looks the way its supposed to.

Pointers much appreciated.

like image 338
Mathias Avatar asked May 02 '13 05:05

Mathias


People also ask

What is Spock in Java?

Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language. Thanks to its JUnit runner, Spock is compatible with most IDEs, build tools, and continuous integration servers.


1 Answers

You can capture an argument as follows:

// must be declared before when-block (or inside Specification.interaction {}) def captured  when: ...  then: 1 * mock.doNetworkCall(...) >> { record, recordClass ->      // save the argument     captured = record     ... } // use the saved argument captured == ... 

That said, often there is a simpler solution such as checking the expected record right in the argument constraint (e.g. ...doNetworkCall( { it == ... } )).

like image 126
Peter Niederwieser Avatar answered Sep 19 '22 03:09

Peter Niederwieser