在Grails Spock规范测试中注入依赖项

Tom*_*ero 12 testing grails groovy dependency-injection spock

我需要在我的测试中获取在我的域对象中注入的依赖项.

此测试放在test/integration目录中并从中扩展spock.lang.Specification.

我怎样才能做到这一点?

注意:我已经看过这篇文章如何将春豆注入spock测试,但它与grails无关.

编辑:

我想要注入的依赖项是springSecurityService在我的SecUser子类中调用的Player.失败的方法是在encodePassword()中调用的方法beforeInsert().

我可以encodePassword()在一些测试中模拟这个方法,但是当我想测试我的控制器方法时save(),我无法模拟Player正在创建的那个,因为它全部发生在controllers方法中.

更改为extend后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
    }

}
Run Code Online (Sandbox Code Playgroud)

我在运行时收到此异常grails test-app :spock:

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

当我单独进行测试时这个:

| 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)
Run Code Online (Sandbox Code Playgroud)

luc*_*e84 12

尝试将springSecurityService声明为测试,就像在控制器中一样.Grails应该为你完成所有的工作:)

对于集成测试,您可以执行以下操作:

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:
         // ....
 }
Run Code Online (Sandbox Code Playgroud)

如果您需要测试特定的域对象(作为您的Fecha类),您可能需要进行单元测试,如下所示:

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:
         // ....
 }
Run Code Online (Sandbox Code Playgroud)

您也可以使用单元测试来测试服务,这取决于您要测试的内容(类 - 服务 - 或"情况" - 服务的使用方式 - ).

PS.当然,此处的代码尚未经过测试,可能包含拼写错误.:)但我希望你明白如何测试.