Zero Argument Closures Prevent Running Test Cases Directly

## Operating System: Windows 10

=======================================

## Katalon Studio Version: 5.5.0

## Katalon Studio logs:

No logs related to the time of the event.

## Steps to reproduce

Create a Test Case

Add a Zero Argument Closure, like so:

{-> println("TEST") }

Attempt to Run the Test Case Directly in Katalon Studio.

## Expected Behavior

All else constant, it should run and print “TEST”

## Actual Behavior

**
**

## Backgroun Information

When a Test Cases contains a zero argument closure, AND you attempt to run the single test case directly in the IDE (without a suite or test collection) this error is presented:

“There are errors in the script. Please fix before Executing”

However, zero argument closures are syntactically valid . Additionally, they compile fine and the same Test Cases can be run from a Test Suite without issue.

At first it may seem like there is no difference here, but some libraries/frameworks monitor the number of arguments a closure is using.

This is the case for my Scripts with Cucumber Step Definitions. I’m unable to run a Test Case directly when debugging, because I must provide a zero argument closure when Cucumber expects one.

Luckily I am able to work around this by housing the Test Case in a Test Suite and running the Test Suite instead of the Test Case directly.

// Zero argument closure. 
{ -> }

// Implicitly 1 argument closure, with 'it' as the argument
{ }

// This is syntactically correct, but Katalon prevents running
Given(~/^Some user$/) { ->	// Write code here	assert true}

// Katalon allows this, 
// Cucumber complains about the 1 implicit 'it' argument
Given(~/^Some user$/) {
	// Write code here	assert true}

error.png

Ahh , I need to update this to the appropriate format. One mommet!

If I’m understanding correctly, the syntax does not require the arrow when there are no parameters. This seems to work:

def x = {
  return false
}

assert x()

See http://groovy-lang.org/closures.html

According to the syntax string, the parameters and arrow -> are optional together.

What is truly annoying is that Katalon does not give any clue as to what is wrong here:

def x = { ->  // extraneous arrow causes Katalon to complain
  return false
}

assert x()

Russ Thomas said:

If I’m understanding correctly, the syntax does not require the arrow when there are no parameters. This seems to work:

def x = {

return false
}

assert x()


  

See http://groovy-lang.org/closures.html

  

According to the syntax string, the parameters and arrow -> are optional _together_.  

  

What is truly annoying is that Katalon does not give any clue as to what is wrong here:

  

def x = { → // extraneous arrow causes Katalon to complain
return false
}

assert x()


  

  

{ } vs. {->} are different.
{ } is implicitly equivalent to { it - > } (i.e accepts 1 parameter)
{-> } accepts 0 parameters.

See Section 2.2 of the link you provided: The Apache Groovy programming language - Closures

This matters in situations where you are passing the closure to other classes/methods/scripts that for one reason or another are expecting a zero argument closure.

It is not extraneous. It is a correct and meaningful usage. Albeit a very specific use case.

That being said, The documentation at the link could be more clear on this.
I too noticed the indication that [argument → ] appeared to be optional together.

But ‘{-> }’ compiles, it works when not running from Katalon Studio as Single Test Case, and it is buried a bit in the Groovy Docs.

It is not extraneous.

Point(s) taken.

It is a correct and meaningful usage. Albeit a very specific use case.

That being said, The documentation at the link could be more clear on this.

Agreed. Complete threw me off.

I too noticed the indication that [argument → ] appeared to be optional together.

I can almost see the comments in the parser/compiler code, “// Ah but if…” :wink:

Thanks for the deep dive, Shane.