1
0
-1

We are using Spock framework for unit test/integration testing and we are setting a Tnx as parameter but we are getting errors while trying to simulate a Txn in the tests. I have the dependencies installed in my local Maven repository. 

There is a correct/proper way to create and simulate a Txn for testing porpuses?

We have the below code in our service:

def setup() {
    this.service = Spy(ApplicationFullDetailsService_v1)
    this.httpResponse = new HttpResponse()
    CayenneUtils.createDataContext()
}


def "invokeOk"() {
    given: 'Request 200 - mock service'
        this.httpResponse.setStatus(HttpStatus.SC_OK)
        this.httpResponse.setContent(OK_RESPONSE.getBytes())
    and: 'PostRequest mock'
        GroovySpy(constructorArgs: [LOCALHOST_URL], PostRequest, global: true) {
            execute() >> this.httpResponse
        }
    and: 'GetPropertyQuery mock '
        GroovySpy(PropertyQuery, global: true) {
            getValue() >> LOCALHOST_URL
        }
    when:
        Txn txn = buildTxn()
        def response = service.invoke(null,txn, unitTestParams,null)
        println "Spock Response: $response"
    then:
		// validate the response
}


Txn buildTxn(){
	//String xml =getXMLMock()
                // Txn(new Submission(), params, xml, groupNames, propertyMap, receiptPDF, fileAttachList)	// We have tried this
    //Txn txn = new Txn(new Submission(), params, xml,new HashSet(), [:], null, null) // and this
    Txn txn = new MockVoBuilder().createTxnOpened()	// like unit test in TM
    txn.clientCode = 'ao'	// tried this but is a final field
    txn
}


And we are getting this error:

dic 28, 2017 5:59:26 PM org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader load
INFO: Loading XML configuration resource from jar:file:/C:/somepath/.m2/repository/com/avoka/avoka-sfm-core/5.1.3/avoka-sfm-core-5.1.3.jar!/cayenne-domain.xml
dic 28, 2017 5:59:26 PM org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader createChildTagHandler
INFO: Loading XML DataMap resource from jar:file:/C:/somepath/.m2/repository/com/avoka/avoka-sfm-core/5.1.3/avoka-sfm-core-5.1.3.jar!/formcenterMap.map.xml
dic 28, 2017 5:59:26 PM org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader createChildTagHandler
INFO: Loading XML DataMap resource from jar:file:/C:/somepath/.m2/repository/com/avoka/avoka-sfm-core/5.1.3/avoka-sfm-core-5.1.3.jar!/transactionHistory.map.xml
dic 28, 2017 5:59:26 PM org.apache.cayenne.map.EntityResolver applyDBLayerDefaults
INFO: added runtime complimentary DbRelationship from client_keystore to client
dic 28, 2017 5:59:26 PM org.apache.cayenne.map.EntityResolver applyObjectLayerDefaults
INFO: added runtime complimentary ObjRelationship from ClientKeystore to Client
dic 28, 2017 5:59:26 PM org.apache.cayenne.log.CommonsJdbcEventLogger logConnect
INFO: Connecting. JNDI path: java:/FormCenterDS
dic 28, 2017 5:59:26 PM org.apache.cayenne.configuration.server.JNDIDataSourceFactory getDataSource
INFO: failed JNDI lookup of DataSource location 'java:/FormCenterDS'
dic 28, 2017 5:59:26 PM org.apache.cayenne.log.CommonsJdbcEventLogger logConnectFailure
INFO: *** Connecting: FAILURE.
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
	at javax.naming.InitialContext.lookup(InitialContext.java:417)
	at org.apache.cayenne.configuration.server.JNDIDataSourceFactory.lookupViaJNDI(JNDIDataSourceFactory.java:75)
	at org.apache.cayenne.configuration.server.JNDIDataSourceFactory.getDataSource(JNDIDataSourceFactory.java:55)
	at com.avoka.core.cayenne.CoreJNDIDataSourceFactory.getDataSource(CoreJNDIDataSourceFactory.java:24)
	at org.apache.cayenne.configuration.server.DelegatingDataSourceFactory.getDataSource(DelegatingDataSourceFactory.java:67)
	at org.apache.cayenne.configuration.server.DataDomainProvider.createAndInitDataDomain(DataDomainProvider.java:204)
	at org.apache.cayenne.configuration.server.DataDomainProvider.get(DataDomainProvider.java:106)
	at org.apache.cayenne.configuration.server.DataDomainProvider.get(DataDomainProvider.java:60)
	at org.apache.cayenne.di.spi.CustomProvidersProvider.get(CustomProvidersProvider.java:38)
	at org.apache.cayenne.di.spi.FieldInjectingProvider.get(FieldInjectingProvider.java:42)
	at org.apache.cayenne.di.spi.DefaultScopeProvider.get(DefaultScopeProvider.java:49)
	at org.apache.cayenne.di.spi.DefaultInjector.getInstance(DefaultInjector.java:107)
	at org.apache.cayenne.di.spi.FieldInjectingProvider.injectMember(FieldInjectingProvider.java:93)
	at org.apache.cayenne.di.spi.FieldInjectingProvider.injectMembers(FieldInjectingProvider.java:58)
	at org.apache.cayenne.di.spi.FieldInjectingProvider.get(FieldInjectingProvider.java:43)
	at org.apache.cayenne.di.spi.CustomProvidersProvider.get(CustomProvidersProvider.java:38)
	at org.apache.cayenne.di.spi.FieldInjectingProvider.get(FieldInjectingProvider.java:42)
	at org.apache.cayenne.di.spi.DefaultScopeProvider.get(DefaultScopeProvider.java:49)
	at org.apache.cayenne.di.spi.DefaultInjector.getInstance(DefaultInjector.java:103)
	at org.apache.cayenne.configuration.CayenneRuntime.getChannel(CayenneRuntime.java:173)
	at com.avoka.core.util.CayenneRuntimeFactory.getRuntime(CayenneRuntimeFactory.java:73)
	at com.avoka.core.util.CayenneUtils.getCayenneRuntime(CayenneUtils.java:404)
	at com.avoka.core.util.CayenneUtils.createDataContext(CayenneUtils.java:349)
	at com.avoka.core.util.CayenneUtils$createDataContext.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
	at ApplicationFullDetails.ApplicationFullDetailsService_v1_Spec.setup(ApplicationFullDetailsService_v1_Spec.groovy:29)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:188)
	at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:84)
	at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:481)
	at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:464)
	at org.spockframework.runtime.BaseSpecRunner.doRunSetup(BaseSpecRunner.java:400)
	at org.spockframework.runtime.BaseSpecRunner$8.invoke(BaseSpecRunner.java:382)
	at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:481)
	at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:464)
	at org.spockframework.runtime.BaseSpecRunner.runSetup(BaseSpecRunner.java:375)
	at org.spockframework.runtime.BaseSpecRunner.runSetup(BaseSpecRunner.java:370)
	at org.spockframework.runtime.BaseSpecRunner.doRunIteration(BaseSpecRunner.java:323)
	at org.spockframework.runtime.BaseSpecRunner$6.invoke(BaseSpecRunner.java:309)
	at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:481)
	at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:464)
	at org.spockframework.runtime.BaseSpecRunner.runIteration(BaseSpecRunner.java:288)
	at org.spockframework.runtime.BaseSpecRunner.initializeAndRunIteration(BaseSpecRunner.java:278)
	at org.spockframework.runtime.BaseSpecRunner.runSimpleFeature(BaseSpecRunner.java:269)
	at org.spockframework.runtime.BaseSpecRunner.doRunFeature(BaseSpecRunner.java:263)
	at org.spockframework.runtime.BaseSpecRunner$5.invoke(BaseSpecRunner.java:246)
	at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:481)
	at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:464)
	at org.spockframework.runtime.BaseSpecRunner.runFeature(BaseSpecRunner.java:238)
	at org.spockframework.runtime.BaseSpecRunner.runFeatures(BaseSpecRunner.java:188)
	at org.spockframework.runtime.BaseSpecRunner.doRunSpec(BaseSpecRunner.java:98)
	at org.spockframework.runtime.BaseSpecRunner$1.invoke(BaseSpecRunner.java:84)
	at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:481)
	at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:464)
	at org.spockframework.runtime.BaseSpecRunner.runSpec(BaseSpecRunner.java:76)
	at org.spockframework.runtime.BaseSpecRunner.run(BaseSpecRunner.java:67)
	at org.spockframework.runtime.Sputnik.run(Sputnik.java:63)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)



Thanks in advance!

    CommentAdd your comment...

    1 answer

    1.  
      2
      1
      0

      Hi Giovanni

      I think you would not be able to run this isolated local scenario, because, you see, the test requires Transact Manager platform being initialized (DB, entities, etc).

      Running unit tests locally, debugging and many more test/continuous integrations capabilities are coming with the recent Transact Manager 17.10.0.

      Regards
      Rado

      1. giovanni carlo

        Radoslav, Thanks for reply. 

        Do you know if there is some place where I can view the documentation update and/or view some examples for TM 17.0.0?


        Thanks and regards, 

      2. Radoslav Ivanov

        You will have to extend a class which will do all the initializations:


        import com.avoka.tm.svc.*
        import com.avoka.tm.test.*
        import com.avoka.tm.util.*
        import com.avoka.tm.vo.*
        import org.junit.*
        
        
        public class DDSTest extends TransactSDKUnitTest {
        
            @Test
            void testAddressFirstLine() throws Exception {
                Txn txn = new MockVoBuilder().createTxnOpened()
                MockRequest request = new MockRequest()
                Map params = [
                    "svcDef": svcDef,
                    "txn": txn,
                    "request": request,
                    "user": null
                ]
                String result = (String) new ServiceInvoker(svcDef)
                    .setLogger(logger)
                    .invoke(params)
                logger.info result
                Path path = new Path(result)
                assert "123 Wall Street" == path.val("address.firstLine")
            }
        }
      3. Jorge Ambriz

        Hi Radoslav

        Currently I´m using Spock framework for unit testing, which requires my test class to extend another one, which means I can´t extend the class that you mention. Furthermore, I don´t really want to initialize Cayenne or anything else just to create a mock vo, as it´s a unit test, i´d like to have the least possible external dependencies. I don´t feel that is the best way to do this

        Still i´d like to take a look to that class, in which jar/package can I find it?

        What I did was to create a simplified version of the Txn class, with the same FQN name, and make sure it´s in the classpath above the real class, so the VM picks mine. But it´s more of a workaround than a solution. I hope we can have something similar that is part of the avoka framework.


      4. Radoslav Ivanov

        Hi 

        In version 5.1.4 and later, there are vo constructors which get Map of fields and can be used only in unit test context. Another approach might be MockIto framework...

        Regards
        Rado

      CommentAdd your comment...