【问题标题】:How we can pass an array to a method using Groovy Script TestStep in SoapUI Pro?我们如何在 SoapUI Pro 中使用 Groovy Script TestStep 将数组传递给方法?
【发布时间】:2014-08-31 22:03:46
【问题描述】:

我不熟悉 带有 SoapUI 的 Groovy 脚本。我必须将数组传递给方法并修改值,然后将它们打印在日志中。为此,我编写了以下脚本。但它没有按预期工作。

String [] countries = new String[5];
void ReuseArray(String[] regions){};

countries[0] = "India";
countries[1] = "Singapore";
countries[2] = "Indonesia";
countries[3] = "Japan";
countries[4] = "Thailand";

for (int i=0; i<5; i++)
{
    log.info("Country :" + countries[i]);
}

ReuseArray(countries);

void ReuseArrayData(String[] regions)
{
    for (int i=0; i<5; i++)
    {
        log.info("Method :" + regions[i]);
    }
}

以上脚本产生以下输出:

IST 2014 年 7 月 10 日星期四 19:08:13:INFO:Welcome
2014 年 7 月 10 日星期四 19:08:13 IST:INFO:Country :India
2014 年 7 月 10 日星期四 19:08:13 IST:INFO:Country :Singapore
2014 年 7 月 10 日星期四 19:08:13 IST:INFO:Country :Indonesia
2014 年 7 月 10 日星期四 19:08:13 IST:INFO:Country :Japan
IST 2014 年 7 月 10 日星期四 19:08:13:INFO:Country :Thailand

如果我错了,请纠正我。

【问题讨论】:

  • 脚本的输出是什么?
  • 按照上面的脚本,它应该显示 5 个国家的名称列表,但它没有显示...
  • 看来这和SoapUI无关,这只是Groovy的问题?此外,您的代码是纯 Java,甚至不是 Groovy ......这很好,因为 Groovy 与 Groovy 的源代码兼容 99%。您的脚本将不会产生Welcome 日志信息消息,但它会产生其他五个Country 消息。您的方法ReuseArrayData() 从未使用过。我看不出问题出在哪里。
  • 那么我们不能在带有 SoapUI 的 Groovy 脚本中使用参数方法吗?

标签: testing groovy scripting automation soapui


【解决方案1】:

在发现错误后,我找到了我的问题的正确答案。这是代码。

log.info("Welcome ")
String [] countries = new String[5];

countries[0] = "India";
countries[1] = "Singapore";
countries[2] = "Indonesia";
countries[3] = "Japan";
countries[4] = "Thailand";

for (int i=0; i<5; i++)
{
    log.info("Country :" + countries[i]);
}

ReuseArrayData(countries);

void ReuseArrayData(String[] regions)
{
    for (int i=0; i<5; i++)
    {
        log.info("Method :" + regions[i]);
    }
}

【讨论】:

    【解决方案2】:

    您可以在 groovy/Java 中使用数组作为函数的参数,但就您的代码而言,似乎 ReuseArray 没有任何定义,并且从未调用过 ReuseArrayData。

    【讨论】:

      猜你喜欢
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多