【问题标题】:How to evaluate a String which one like a classname.methodname in SoapUI with Groovy?如何使用 Groovy 在 SoapUI 中评估类似于 classname.methodname 的字符串?
【发布时间】:2019-05-23 15:56:41
【问题描述】:

我有如下的 groovy 代码:

def randomInt = RandomUtil.getRandomInt(1,200);
log.info randomInt

def chars = (("1".."9") + ("A".."Z") + ("a".."z")).join()
def randomString = RandomUtil.getRandomString(chars, randomInt)   //works well with this code
log.info  randomString


evaluate("log.info new Date()")

evaluate('RandomUtil.getRandomString(chars, randomInt)')   //got error with this code

我想在 SoapUI 中使用 Groovy 评估一个类似于 {classname}.{methodname} 的字符串,就像上面一样,但是在这里出现错误,如何处理这个问题并让它像我期望的那样工作?

我已经试过了:

evaluate('RandomUtil.getRandomString(chars, randomInt)')   //got error with this code

错误如下:

CST 2019 年 5 月 23 日星期四 22:26:30:错误:发生错误 [No such property: getRandomString(chars, randomInt) for class: com.hypers.test.apitest.util.RandomUtil],请参阅错误日志详情

【问题讨论】:

    标签: groovy


    【解决方案1】:

    以下代码:

    log = [info: { println(it) }]
    
    class RandomUtil { 
      static def random = new Random()
    
      static int getRandomInt(int from, int to) {
        from + random.nextInt(to - from)
      }
    
      static String getRandomString(alphabet, len) {
          def s = alphabet.size()
          (1..len).collect { alphabet[random.nextInt(s)] }.join()
      }
    }
    
    randomInt = RandomUtil.getRandomInt(1, 200)
    log.info randomInt
    
    chars = ('a'..'z') + ('A'..'Z') + ('0'..'9')
    
    def randomString = RandomUtil.getRandomString(chars, 10)   //works well with this code
    log.info  randomString
    
    
    evaluate("log.info new Date()")
    
    evaluate('RandomUtil.getRandomString(chars, randomInt)')   //got error with this code
    

    模拟您的代码,运行,并在运行时产生以下输出:

    ~> groovy solution.groovy
    70
    DDSQi27PYG
    Thu May 23 20:51:58 CEST 2019
    
    ~> 
    

    我编写了 RandomUtil 类,因为您没有包含它的代码。

    我认为您看到错误的原因是您定义了变量 charrandomInt 使用:

    def chars = ... 
    

    def randomInt = ...
    

    这会将变量放在本地脚本范围内。请参阅this stackoverflow answer 以获取说明,其中包含指向将事物放入脚本全局范围的不同方法的文档的链接以及如何工作的说明。

    本质上,您的 groovy 脚本代码隐含地是 groovy Script class 的一个实例,而 Binding instance 又与它相关联。当您编写 def x = ... 时,您的变量是本地范围的,当您编写 x = ...binding.x = ... 时,变量是在脚本绑定中定义的。

    evaluate 方法使用与隐式脚本对象相同的绑定。所以我上面的例子起作用的原因是我省略了def,只输入了chars =randomInt =,这将变量放在脚本绑定中,从而使它们可用于evaluate表达式中的代码。

    尽管我不得不说,尽管如此,措辞No such property: getRandomString(chars, randomInt) 对我来说似乎真的很奇怪......我本来希望No such methodNo such property: chars 等。

    分享RandomUtil 的代码可能会有所帮助。

    【讨论】:

    • 如果RandomUtil 类和调用代码在同一个脚本文件中,它工作得很好,但是如果我将RandomUtil 放在一个名为 RandomUtil.groovy 的单个文件中,并带有包 @ 987654344@,然后使用package com.test.apitest.util在调用脚本中包含这个类,它会像No such property: getRandomString(chars, randomInt)一样抛出异常
    • RandomUtil.groovy ``` package com.test.apitest.util class RandomUtil { static def getRandomInt(def min, def max){ return Math.abs(new Random ().nextInt() % max) + min } static def getRandomString(def customString, def length){ def randomString = new Random().with { (1..length).collect { customString[ nextInt( customString.length( ) ) ] }.join() } 返回随机字符串 } } ```
    • 看起来我得到了解决方案,使用完整的包路径包括评估字符串:evaluate("com.test.apitest.util.RandomUtil.getRandomString(chars, randomInt)")
    • 啊哈...好吧,这是有道理的。很高兴你明白了。
    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多