Remo
Remo

Reputation: 604

Validating multiple method arguement in spock

I'm mocking the transformer call like below

3 * transformer.transform(_ as Traveler, _ as Map<String, String>, _ as List<Train>) >>> [expectedObject[0], expectedObject[1], expectedObject[2]]

able to mock successfully.

Now, want to assert the 2nd arguement which is Map<String, String>.

Tried like below

3 * transformer.transform(_ as Traveler, _ as Map<String, String>, _ as List<Train>) >> {
            Map<String, String> test->
                assert test['abcd'] == '1234'

                test
}

But not able to assert. How to assert this?

Upvotes: 0

Views: 970

Answers (1)

Leonard Br&#252;nings
Leonard Br&#252;nings

Reputation: 13222

Have a look at argument constraints in your case the code argument constraint combined with type constraint is what you want.

I've created a MCVE based on the limited code that you provided.

interface Transformer {
    String transform(Map<String, String> argument)
}

class ASpec extends Specification {
    def "code argument constraint"() {
        given:
            Transformer transformer = Mock()
        
        when:
            transformer.transform([a: 'hello', b: 'world'])
        
        then:
            1 * transformer.transform({ it.b == 'world'} as Map<String, String>) >> 'Hello World'
    }
}

Try it in the Groovy Web Console

Upvotes: 1

Related Questions