mercredi 18 septembre 2019

How to set variables after an expected mock method call in THEN stage in Spock Groovy?

I want to test my Java service with Spock Groovy, and the service is a bit tricky, because a method call returns a value and also changes the content of the variable which was passed to the method.

Since I want to mock this method call, I need to tell Spock somehow that when the method was executed, return a specific value AND change the content of the passed variable.

Something like this:

given: "list with one element"
List<String> list = ["mock"]

when: "executing the service call"
service.execute(list)

then: "external method is called, which updates the passed variable and returns true as success"
1 * external.addOneMoreToList(list) >> true
and: "list is updated successfully"
list.size == 2
list.get(1) == "mock 2"

But I don't know where and how to update the list by adding the next element: "mock 2".

I know this example does not make sense, but I didn't want to write my real testcase, because it is huge, but I need similar ad-hoc-update

Aucun commentaire:

Enregistrer un commentaire