2012-03-14 8 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のように見えるクラスローダーの問題を... –

+0

それはEclipseの設定だったようだ。 Groovy Eclipseは、GroovyStarterを使用した起動設定を生成しました。コマンドラインでjavaを直接呼び出して成功した後、私はスクリプトを直接呼び出すように起動設定を変更し、正常に動作します。 GroovyStarterは動作する予定ですか? GroovyStarterの目的は、メイン・メソッドを持つクラスに対してスクリプトをコンパイルする場合です。 –

答えて

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がスローされます!