Mahbub Rahman Mahbub Rahman - 2 months ago 9
Groovy Question

Groovy: pop the last element from a string list

In my spock test class, I have the following two lists:

@Shared def orig_list = ['東京(成田・羽田)', '日本','アジア' ]
@Shared def dest_list = ['ソウル', '韓国','アジア' ]


def "Select origin"()
{
when:
something()

then:
do_something()


where:
area << orig_list.pop()
country << orig_list.pop()
port << orig_list.pop()
dest_area << dest_list.pop()
dest_country << dest_list.pop()
dest_port << dest_list.pop()
}


But getting the error:

java.lang.IllegalArgumentException: Couldn't select option with text or value: ア....


But If I do not use where block and do like:

def "Select origin"()
{
def area = orig_list.pop()
def country = orig_list.pop()
def port = orig_list.pop()

def dest_area = dest_list.pop()
def dest_country = dest_list.pop()
def dest_port = dest_list.pop()

when:
something()

then:
do_something()
}


Than it works fine.

How can I get the values in where block from list? What was the problem?

Answer

The variables defined in the where block expects lists, but the pop() method returns an element in your list, which in your case seems to be a string.

Either wrap the list.pop() in brackets, like this [list.pop()] or, perhaps better, rewrite your where block to use the column syntax, i.e. something like this:

    where:
    area | country | port | dest_area | dest_country | dest_port
    'a1' | 'c1'    | 'p1' | 'da1'     | 'dc1'        | 'dp1'
    'a2' | 'c2'    | 'p2' | 'da2'     | 'dc2'        | 'dp2'
Comments