0

I am learning Groovy to use it in Soap UI.

I want to know the basic difference between [] and () and where it would be used.

[] - I understand it is an array..

Typical eg.,

() usage:

def r = testRunner.testCase.testSuite.getTestCaseByName("Session").getTestStepByName("InvalidLoginAttempt").run(testRunner, context);

[] usage:

def r = testRunner.testCase.testSuite.testCases["Login"].getTestStepByName("InvalidLoginAttempt").run(testRunner, context);
doelleri
  • 19,232
  • 5
  • 61
  • 65
ChanGan
  • 4,254
  • 11
  • 74
  • 135

1 Answers1

3

Welcome to Groovy programming!

When [] is used beside an object it invokes the underlying getAt() method of an object.

The parenthesis are used to call a method, so it can be used to invoke a getAt method. It varies from implementation.

You can think of it as a syntactic sugar so you don't have to call the whole method. For example, on arraylists:

def list = [10, 20, 30, 40]

assert list[2] == 30
assert list.getAt(3) == 40
assert list.get(0) == 10

They all work, but the [] notation is shorter.

My bet is that SoapUI::TestCases getAt probably invokes getTestCaseByName, so they are alias to the same operation.

Also worth noting: the [], when assigned to a variable, creates an ArrayList. When used with a equal sign (list[0] = 90) it invokes the setAt() method of an object/collection.

Will
  • 14,348
  • 1
  • 42
  • 44
  • where [] is used and where () would be used? – ChanGan Feb 16 '13 at 11:37
  • You'd use `[]` in objects and collections for a shorter syntax; You can also use `()` for invoking the method, but that'd be a little longer. `[]` are usually more idiomatic in Groovy. – Will Feb 16 '13 at 12:24
  • You have to be sure the underlying lib supports both. You can usually check the soapui groovy documentation (http://stackoverflow.com/questions/6106336/soapui-groovy-api-documentation) – Will Feb 16 '13 at 12:26