テストで自分のドメインオブジェクトに挿入された依存関係を取得する必要があります。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)
'@ AutoWired'、多分?テストを実行する際、grailsアプリケーションが実行されていますか? – Will
これらの依存関係を模擬する方が良いでしょうか?どんな依存関係が注入されないのですか?問題を示すためのコード例をいくつか書いてください。 –
Grailsで@Autowiredを使ったことはありませんでした。私はそれを試みます。 テストを実行するときに私はアプリを実行していません。 –