2

I have a test case where i need to test multiple strings and they are initialised as String[]

   myArray.each {
        shouldFail(IllegalArgumentException) {
            println "it = " + it
            testObj.testMyString("$prefix $it", mockBuilder);
         }
    }

the print statement shows that "it" is null. What am I doing wrong?

3 Answers 3

3

If you name your each var, it should work:

myArray.each { element ->
    shouldFail(IllegalArgumentException) {
        println "it = $element"
        testObj.testMyString("$prefix $element", mockBuilder)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Each closure has its own "it". In your case when "it" was null, it was shouldFail closure's "it" and not myArray.each's closure.

Comments

0

it worked when I changed the code to this

 myArray.each {
        def testStr = it
        shouldFail(IllegalArgumentException) {
            println "it = " + testStr
            testObj.testMyString("$prefix $testStr", mockBuilder);
         }
    }

I guess "it" is not available in inner closures

1 Comment

It's not that it's not available, it's just a different "it".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.