2012-08-13 90 views
12

我需要在我的测试中获取注入到我的域对象中的依赖项。在Grails Spock规范测试中注入依赖项

此测试放置在测试/集成目录中,并从spock.lang.Specification扩展。

我该如何做到这一点?

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

编辑:

我想获得注入的依赖性在我SecUser子类,称为PlayerspringSecurityService。失败的方法是encodePassword(),在beforeInsert()中调用该方法。

我可以在一些测试中嘲笑这个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) 

而这一次,当我单独运行测试:

| 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 2012-08-13 11:55:02

+0

也许这是嘲笑这些依赖的更好的主意?什么依赖不被注入?写一些示例代码来显示问题。 – 2012-08-13 12:59:40

+0

我从来没有尝试过在Grails中使用@Autowired,我会尝试一下。 我在运行测试时没有运行该应用程序。 – 2012-08-13 15:39:41

回答

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: 
     // .... 
} 

如果你需要测试特定的域对象(如你的出生日期类),你可能需要一个单元测试,这样的事情:

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服务 - 或“情况” - 服务的使用方式 - )。

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

+0

实际上,我扩展了Specification而不是IntegrationSpec。 当我试图改变延长IntegrationSpec,我运行测试时出现以下情况例外: 显示java.lang.NullPointerException:在grails.plugin.spock.IntegrationSpec无法获取财产“mainContext”空对象 \t上$ spock_initializeSharedFields(IntegrationSpec.groovy) – 2012-08-16 03:38:49

+0

您应该在进行集成测试时扩展IntegrationSpec https://code.google.com/p/grails-spock-examples/wiki/Overview#Integration_tests 是否有一点要声明null mainContext ?你目前使用哪个版本的spock插件?你能添加一段代码来看看吗? – lucke84 2012-08-16 08:59:15

+0

我正在使用插件“:spock:0.6”。我从不声明mainContext作为变量。我已经用我的测试代码更新了这个问题 – 2012-08-17 01:16:21

1

以上接受的答案适用于需要注入服务类的控制器的单元测试。

如果您在资源中定义了其他弹簧托管bean。Groovy和需要他们注入你可以在你的规范类的顶部添加此行

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

来源http://grails.github.io/grails-doc/2.4.4/guide/testing.html#unitTesting

添加静态loadExternalBeans =真字段定义为单元测试类使的Grails单元测试运行时加载grails-app/conf/spring/resources.groovy和grails-app/conf/spring/resources.xml文件中的所有bean定义。