gpt4 book ai didi

testing - 在 Grails Spock 规范测试中注入(inject)依赖项

转载 作者:行者123 更新时间:2023-11-28 19:41:46 25 4
gpt4 key购买 nike

我需要在我的测试中将依赖项注入(inject)到我的领域对象中。

此测试位于 test/integration 目录中,并扩展自 spock.lang.Specification

我怎样才能做到这一点?

注意:我看过这个帖子How to inject spring beans into spock test , 但它与 grails 无关。

编辑:

我想要注入(inject)的依赖项是 springSecurityService,在我的 SecUser 子类中,名为 Player。失败的方法是 encodePassword(),它在 beforeInsert() 中调用。

我可以在某些测试中模拟这个 encodePassword() 方法,但是当我想测试我的 Controller 方法 save() 时,我不能模拟 Player 正在创建,因为这一切都发生在 Controller 方法内。

更改为扩展 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)

这是我单独运行测试时的结果:

| 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)

最佳答案

尝试将 springSecurityService 声明到测试中,就像在 Controller 中所做的那样。 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:
// ....
}

如果您需要测试特定领域对象(如您的 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:
// ....
}

您也可以使用单元测试来测试服务,这取决于您要测试什么(类 - 服务 - 或“情况” - 服务的使用方式 -)。

附言。当然,这里的这段代码还没有经过测试,可能包含拼写错误。 :) 但我希望你明白我关于如何测试的观点。

关于testing - 在 Grails Spock 规范测试中注入(inject)依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11928252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com