1
votes

I have an integration test. Some code from it:

Test1 {
        static closure1 = { Class1 resultState, Class1 originState ->
            with(resultState) {
                name == originState.name
                breed == originState.breed
                bornDate == originState.bornDate
            }
        }

        @Unroll
        def 'exercise #methodName'() {
            ...
            expected(resultState, originState)
            ...
            methodName  || expected
            'name1'     || closure1
        }
}

I get an groovy.lang.MissingMethodException: No signature of method: Test1$__clinit__closure10.with() is applicable for argument types: (Class1, Test1$__clinit__closure10$_closure12) values: [//values].

But when I refactor my code to form with method instead of closure, everything is fine.

    Test1 {

            void method1(Class1 resultState, Class1 originState) {
                with(resultState) {
                    name == originState.name
                    breed == originState.breed
                    bornDate == originState.bornDate
                }
            }

            @Unroll
            def 'exercise #methodName'() {
                ...
                expected(resultState, originState)
                ...
                methodName  || expected
                'name1'     || closure1
             }


        }

But why?? In closure-from I'm getting the exception in with-block, not in closure call. In my other test form of test with closures (but without with-block) works fine. Type of closure here - Closure, in other test - Closure, if it's important.

What's wrong in my code??

1
Can you post a full test class so we can reproduce same exception? The code you have posted does not fulfill Minimal, Complete, and Verifiable example - Szymon Stepniak

1 Answers

2
votes

One issue is that with is a method on Specification, but you have it in a static context, so at this point, there is no with method to call...

Another is that this is outside of a spec method, so the a == b lines will not be asserted, as the spock dsl won't pick them up...

One solution is to make it a method, and pass in a method handle, and assert your values and return true if they all pass:

private test1(resultState, originState) {
    with(resultState) {
        assert name == originState.name
        assert breed == originState.breed
        assert bornDate == originState.bornDate
    }
    true
}

@Unroll
def 'exercise #methodName'() {

    ...
    then:
    expected(resultState, originState)

    where:
    methodName  || expected
    'name1'     || this.&test1
}