2012-08-13 5 views
12

テストで自分のドメインオブジェクトに挿入された依存関係を取得する必要があります。Grails Spock仕様の依存関係を教えてください

このテストはテスト/統合ディレクトリに配置され、spock.lang.Specificationから拡張されています。

どうすればこの問題を解決できますか?

注:この投稿はHow to inject spring beans into spock testですが、grailsとは関係ありません。

編集:

私が注入された取得したい依存関係がPlayerと呼ばれる私のSecUserサブクラスでspringSecurityServiceです。失敗している方法は、beforeInsert()で呼び出されるencodePassword()です。

私はいくつかのテストでは、このencodePassword()方法を模擬することができますが、私は私のコントローラメソッドsave()をテストしたいとき、私はそれがすべてのコントローラのメソッドの内部で起こるために作成されているPlayerを模擬することはできません。

IntegrationSpecを拡張するために変更した後、これは私のテストコードです:

package intertigre.test.domain 
import intertigre.domain.Fecha; 
import intertigre.test.util.DomainFactoryTestService 
import grails.plugin.spock.IntegrationSpec 
import grails.test.mixin.TestFor 

    @TestFor(Fecha) 
    class FechaSpec extends IntegrationSpec{ 

    DomainFactoryTestService domainFactoryTestService = new DomainFactoryTestService() 

    def 'test'(){ 
     given: 
      def fecha = new Fecha() 
     when: 
      fecha.save() 
     then: 
      Fecha.get(1) == fecha 
    } 

} 

grails test-app :spockを実行しているとき、私はこの例外を取得しています:

java.lang.NullPointerException: Cannot get property 'mainContext' on null object 
    at grails.plugin.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy) 

そして、この1、私は一人でテストを実行すると:

| Failure: intertigre.test.domain.FechaSpec 
| java.lang.NullPointerException: Cannot get property 'autowireCapableBeanFactory' on null object 
    at grails.plugin.spock.IntegrationSpec.setupSpec(IntegrationSpec.groovy:47) 
| Failure: intertigre.test.domain.FechaSpec 
| java.lang.NullPointerException: Cannot invoke method isActive() on null object 
    at grails.test.mixin.support.GrailsUnitTestMixin.shutdownApplicationContext(GrailsUnitTestMixin.groovy:232) 
    at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176) 
    at org.spockframework.runtime.extension.builtin.JUnitFixtureMethodsExtension$FixtureType$FixtureMethodInterceptor.intercept(JUnitFixtureMethodsExtension.java:145) 
    at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:84) 
    at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176) 
+0

'@ AutoWired'、多分?テストを実行する際、grailsアプリケーションが実行されていますか? – Will

+0

これらの依存関係を模擬する方が良いでしょうか?どんな依存関係が注入されないのですか?問題を示すためのコード例をいくつか書いてください。 –

+0

Grailsで@Autowiredを使ったことはありませんでした。私はそれを試みます。 テストを実行するときに私はアプリを実行していません。 –

答えて

12

springSecurityServiceをあなたがコントローラでやっているように、テスト。 Grailsのは、あなたのためのすべての仕事をすることになっている:)

あなたはこのような何か統合テストのために:あなたは(あなたのFechaクラスなど)は、特定のドメインオブジェクトをテストする必要がある場合は

package intertigre.test.domain 
import intertigre.domain.Fecha; 
import intertigre.test.util.DomainFactoryTestService 
import grails.plugin.spock.IntegrationSpec 

class DomainFactoryTestServiceSpec extends IntegrationSpec{ 

def domainFactoryTestService // you dont need to create a new instance, it's injected by spring 

def 'test'(){ 
    given: 
     // ... 
    when: 
     // ... 
    then: 
     // .... 
} 

を、あなたはおそらく、ユニットテスト、このような何か必要があります:あなたは、同様のサービスをテストするためにユニットテストを使用することができます

package intertigre.test.domain 
import intertigre.domain.Fecha 
import intertigre.test.util.DomainFactoryTestService 
import grails.test.mixin.TestFor 
import grails.test.mixin.Mock 
import spock.lang.Specification 

@TestFor(Fecha) 
@Mock([OtherObject1, OtherObject2]) 
class FechaSpec extends Specification { 

def domainFactoryTestService // same thing here, if you need the service 

def 'test'() { 
    given: 
     def fecha = new Fecha() 
    and: 
     def oo1 = new OtherObject1() 
    when: 
     // ... 
    then: 
     // .... 
} 

を、それはあなたが(クラス-the service-や「状況」をテストしようとしているかに依存します - サービスの利用方法 - )。

ps。もちろん、ここのコードはテストされておらず、タイプミスを含んでいる可能性があります。 :)しかし、私はあなたがテストする方法について私のポイントを得ることを願っています。

+0

私は実際にIntegrationSpecの代わりにSpecificationを拡張しています。 私はIntegrationSpecを拡張するために変更しようとするとテストを実行しているとき、私は次の例外を取得: のjava.lang.NullPointerException:。grails.plugin.spock.IntegrationSpecでヌルオブジェクト \tにプロパティ「mainContext」を取得できません$ spock_initializeSharedFields(IntegrationSpec.groovy) –

+0

統合テスト中にIntegrationSpecを拡張する必要があります。https://code.google.com/p/grails-spock-examples/wiki/Overview#Integration_tests nullのmainContextを宣言するポイントがありますか?現在使用しているSpockプラグインのバージョンは?あなたは一見するためにコードを追加できますか? – lucke84

+0

私はプラグイン ":spock:0.6"を使用しています。 mainContextを変数として宣言することは決してありません。私はテストコードで質問を更新しました –

1

上記の応答は、サービスクラスを注入する必要があるコントローラの単体テストに適しています。

リソースに他のスプリングマネージドBeanを定義した場合。ユニットテストクラスに静的loadExternalBeans =真のフィールド定義を追加http://grails.github.io/grails-doc/2.4.4/guide/testing.html#unitTesting

のGrailsを作る:Groovyとそれらを必要は

static loadExternalBeans = true //<-- loads resources.groovy beans 

ソースはあなたの仕様クラスの先頭に次の行を追加することができます注入し単体テストランタイムは、すべてのbean定義をgrails-app/conf/spring/resources.groovyとgrails-app/conf/spring/resources.xmlファイルからロードします。

関連する問題