2012-03-14 63 views
4

我正在嘗試運行Geb庫的基本示例(http://www.gebish.org/manual/current/intro.html#introduction)。下面是代碼:Groovy無法通過關閉

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: [[email protected]] 
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: [[email protected]] 
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腳本可以正常工作,但傳遞閉包給方法總會返回類似的錯誤。

+1

如果你在Eclipse之外運行它,它會工作嗎?看起來像Eclipse給你類加載器的問題... – 2012-03-15 09:14:52

+0

它看起來像是Eclipse的配置。 Groovy Eclipse生成了一個使用GroovyStarter的啓動配置。在命令行直接成功調用java後,我改變了啓動配置,直接調用我的腳本,它工作正常。 GroovyStarter是否有望運作?如果腳本編譯爲帶有主方法的類,GroovyStarter的目的是什麼? – 2012-03-15 13:53:10

回答

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!