4

我正在尝试为 Geb 库(http://www.gebish.org/manual/current/intro.html#introduction)运行一个基本示例。这是代码:

import geb.Browser

Browser.drive {
   go "http://google.com/ncr"

    // make sure we actually got to the page
    assert title == "Google"

    // enter wikipedia into the search field
    $("input", name: "q").value("wikipedia")

    // wait for the change to results page to happen
    // (google updates the page dynamically without a new request)
    waitFor { title.endsWith("Google Search") }

    // is the first link to wikipedia?
    def firstLink = $("li.g", 0).find("a.l")
    assert firstLink.text() == "Wikipedia"

    // click the link 
    firstLink.click()

    // wait for Google's javascript to redirect to Wikipedia
    waitFor { title == "Wikipedia" }
}

当我尝试运行它时(使用 Eclipse 的 groovy 支持),我得到以下异常:

Caught: groovy.lang.MissingMethodException: No signature of method: static geb.Browser.drive() is applicable for argument types: (ExampleScript$_run_closure1) values: [ExampleScript$_run_closure1@2a62610b]
Possible solutions: drive(groovy.lang.Closure), drive(geb.Browser, groovy.lang.Closure), drive(geb.Configuration, groovy.lang.Closure), drive(java.util.Map, groovy.lang.Closure), print(java.lang.Object), print(java.io.PrintWriter)
groovy.lang.MissingMethodException: No signature of method: static geb.Browser.drive() is applicable for argument types: (ExampleScript$_run_closure1) values: [ExampleScript$_run_closure1@2a62610b]
Possible solutions: drive(groovy.lang.Closure), drive(geb.Browser, groovy.lang.Closure), drive(geb.Configuration, groovy.lang.Closure), drive(java.util.Map, groovy.lang.Closure), print(java.lang.Object), print(java.io.PrintWriter)
at ExampleScript.run(ExampleScript.groovy:21)

我认为这是说我传递给静态 Browser.drive 方法的闭包与类型不兼容groovy.lang.Closure,但我不知道为什么。简单的 groovy hello world 脚本可以正常工作,但是将闭包传递给方法总是会返回类似的错误。

4

1 回答 1

2

抄袭自: http: //groovy.codehaus.org/Differences+from+Java

Java 程序员习惯于用分号结束语句并且没有闭包。类定义中还有实例初始化器。所以你可能会看到类似的东西:

class Trial {
  private final Thing thing = new Thing ( ) ;
  { thing.doSomething ( ) ; }
}

许多 Groovy 程序员避免使用分号,因为它会分散注意力和多余(尽管其他人一直使用它们——这是编码风格的问题)。导致困难的情况是在 Groovy 中将上述内容编写为:

class Trial {
  private final thing = new Thing ( )
  { thing.doSomething ( ) }
}

这将引发 MissingMethodException!

于 2012-05-03T22:46:17.950 回答