cancel
Showing results for 
Search instead for 
Did you mean: 

How to write unit test for groovy script with messageLogFactory?

tobiasz_h
Active Participant
1,249

Hello,

I use unit tests for groovy scripts, as shown here:
- https://blogs.sap.com/2017/10/06/how-do-you-test-your-groovy-scripts/
- https://blogs.sap.com/2018/01/24/cpis-groovy-meets-spock-to-boldly-test-where-none-has-tested-before...

In the script I want to log some information (I use messageLogFactory.getMessageLog). At the time of test execution I get an error:
groovy.lang.MissingPropertyException: No such property: messageLogFactory for class: build-query-params

How can I get rid of the error? How can I inject messageLogFactory into the message?

Best Regards,
Tobiasz

View Entire Topic
tobiasz_h
Active Participant

Somehow I managed to achieve what I wanted (with the help of a colleague ;)).
A helpful resource is here:
http://docs.groovy-lang.org/latest/html/documentation/guide-integrating.html#_sharing_data_between_a...

And below is my sample implementation of the cpi test:

package src.test.groovy

import com.sap.gateway.ip.core.customdev.processor.MessageImpl
import com.sap.gateway.ip.core.customdev.util.Message
import com.sap.it.api.msglog.MessageLog
import com.sap.it.api.msglog.MessageLogFactory
import spock.lang.Shared
import groovy.transform.Field
//import com.sap.it.op.agent.mpl.factory.impl.MessageLogFactoryImpl



class SpocSpecBuildQueryParams extends spock.lang.Specification {
@Shared
GroovyShell shell = new GroovyShell()
@Shared
Script script
@Shared
File testFile1

private Message msg

def setupSpec() {
// Load Groovy Script
MessageLogFactory messageLogFactory
def sharedData = new Binding()
messageLogFactory = new MessageLogFactoryImpl()
sharedData.setVariable("messageLogFactory", messageLogFactory)
script = shell.parse(new File("src/main/resources/script/build-query-params.groovy"))
script.setBinding(sharedData)
testFile1 = new File('src/test/groovy/in02.xml')
}

def setup() {
this.msg = new MessageImpl()
}

def "example test - message body is unchanged"() {

given: "the message body is provided with initial value"
this.msg.setBody(testFile1.text)

when: "we execute the Groovy script"
script.processData(this.msg)

then: "the message body is the same as input message body"
this.msg.getBody() == testFile1.text

}

}

class MessageLogFactoryImpl implements MessageLogFactory {

@Override
MessageLog getMessageLog(Object o) {
return new MessageLogImpl()
}
}

class MessageLogImpl implements MessageLog {

@Override
void setStringProperty(String s, String s1) {

}

@Override
void setIntegerProperty(String s, Integer integer) {

}

@Override
void setLongProperty(String s, Long aLong) {

}

@Override
void setBooleanProperty(String s, Boolean aBoolean) {

}

@Override
void setFloatProperty(String s, Float aFloat) {

}

@Override
void setDoubleProperty(String s, Double aDouble) {

}

@Override
void setDateProperty(String s, Date date) {

}

@Override
void addAttachmentAsString(String s, String s1, String s2) {

}

@Override
void addCustomHeaderProperty(String s, String s1) {

}
}
jvallejocollados
Discoverer
0 Kudos
Thank you.