Invoking closure/method from inside setup method

For some reason, setup method doesn’t recognize the closure that came before it, because when this code gets put into a test suite and ran, it throws
MissingMethodException about the closure I’m trying to call:

`

int initialNumberOfRows = 0

def findRowCount = {

initialNumberOfRows = 5
}

/**

  • Some methods below are samples for using SetUp/TearDown in a test suite.
    /
    /
    *
  • Sets up test environment.
    */
    @SetUp(skipped = false) // Please change skipped to be false to activate this method.
    def setUp() {
    // login and select the test company first
    WebUI.callTestCase(findTestCase(‘Test Cases/TestCompanySelectGoesToDashboard’), [(‘shouldLogout’) : false, (‘shouldCloseBrowser’) : false])
    // go to the “Discounts” page
    WebUI.click(findTestObject(‘PageMenuOptions/a_Discounts’))
    // determine the row count
    WebUI.delay(5)
    findRowCount()
    }

`

I run that code and get greeted with the following exception message:

`

groovy.lang.MissingMethodException: No signature of method: DiscountsSuite.findRowCount() is applicable for argument types: () values:

`

The closure, as of right now, is a stub, but will be used to get the number of rows, after which we will delete as cleanup for the test suite (I make the rows via the test suite, I gotta clean em up!)

Hi Michael,

findRowCount() is not a method (or at least it is not defined as method). So you cannot call it from your test. You must redefine your code according what you want to do.

I defined it as a closure (as closures can access members that are defined outside of them).

Indeed. But you have to define a method like this:

def int findRowCount() {	int initialNumberOfRows = 5        return initialNumberOfRows }

Then, you can call findRowCount() which returns 5 to you.

That defeats the whole purpose of what I’m trying to do: get initialNumberOfRows before any tests start in a test suite, and then, after all tests have run, after that initialNumberOfRows delete each row one by one.

The table’s state, namely its row count, needs to be saved somewhere in the script to the test suite, and that means outside the method definitions.

You might want to save the variable externally (maybe in file?) Then, you can use FileWriter and FileReader for writing/reading from file.

That sounds like a lot for just one number…