मुझे अपने परीक्षणों में मेरे डोमेन ऑब्जेक्ट्स में इंजेक्शन देने की आवश्यकता है।Grails में निर्भरता इंजेक्शन स्पॉक विशिष्टता परीक्षण
यह परीक्षण परीक्षण/एकीकरण निर्देशिका में रखे गए हैं और spock.lang.Specification
से फैले हुए हैं।
मैं इसे कैसे प्राप्त कर सकता हूं?
नोट: मैंने यह पोस्ट How to inject spring beans into spock test देखा है, लेकिन यह grails से संबंधित नहीं है।
संपादित करें:
निर्भरता मैं इंजेक्शन प्राप्त करना चाहते हैं मेरी SecUser
उपवर्ग Player
बुलाया में springSecurityService
है। असफल होने वाली विधि encodePassword()
है, जिसे beforeInsert()
में कहा जाता है।
मैं कुछ परीक्षणों में इस encodePassword()
विधि नकली सकता है, लेकिन जब मैं अपने नियंत्रकों विधि save()
परीक्षण करना चाहते हैं, मैं Player
कि क्योंकि यह सब नियंत्रकों विधि के अंदर होता है बनाया जा रहा है नकली नहीं कर सकते।
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)
और यह एक जब मैं परीक्षण अकेले चलाने
IntegrationSpec
विस्तार करने के लिए बदलने के बाद, यह अपने परीक्षण कोड है:
| 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)
'@ ऑटोवायर', हो सकता है? जब आप अपने परीक्षण चलाते हैं तो क्या आपके grails एप्लिकेशन चल रहा है? – Will
शायद इन निर्भरताओं का नकल करना बेहतर विचार है? क्या निर्भरता इंजेक्शन नहीं है? एक समस्या दिखाने के लिए कुछ उदाहरण कोड लिखें। –
मैंने कभी Grails में @Autowired का उपयोग करने की कोशिश नहीं की है, मैं इसे आज़माउंगा। जब मैं परीक्षण चलाता हूं तो मैं ऐप नहीं चला रहा हूं। –