2012-08-13 57 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定義。