Skip to Content

It all started with a basic (yet very important) question:-

Do you test your Groovy scripts?

Or more accurately, How do you test your Groovy scripts?

Because

if(!software.isTestable()) {
  software = null;
}

 

WebUI, SchmebUI

If we leave it to chance, life with SAP Cloud Platform Integration would be colorless like this:-

How do you even work with such an editor in this day and age… let alone test anything on it??

Well, on occasions like this, pardon me, but I would rather take chance into my own hands, and have a bit more color in my life (code completion and on-the-fly syntax check, anyone?):-

 

Ok, Eclipse is Groovier, but how do we test?

Well, before Vadim Klimov took matters into his own hands, we would have had to rely on creating our own versions of Cloud Integration’s libraries using techniques like Reflecting on the Message class by Morten Wittrock.

But fortunately for all of us, Vadim bravely trailblazed the way forward with Dark Side of Groovy Scripting: Behind the Scenes of Cloud Integration Runtime.

Before you even continue, you will really need to read thoroughly the above blog, as that is both prerequisite and key.

You will also need the below:-

Eclipse Neon (What do you mean you don’t have it yet?!)

Groovy Eclipse (Eclipse is Groovier with Groovy)

 

Let the testing begin!

All ready now?

Let’s first create a Groovy Project.

 

After that, let’s play nice and have a decent package for your Groovy scripts. You will need two scripts here:-

  • The Groovy script that will be used in your integration flow
  • The script that will test the above script

For simplicity, I will just copy over the default Groovy script that is generated from Cloud Integration, namely script1.groovy below.

For the other script, I will just create a new Groovy class named GroovyScriptTest.groovy.

 

All good so far? Let’s replace the code in the testing class with the following code. It’s just a very basic logic to showcase the approach. Essentially what it does is:-

  • Load the script to be tested
  • Initialize the message to be passed into the script
  • Execute the script to be tested
  • Display the results via console

Nothing fancy, but I encourage you to at least try to understand what each part does (there’s always Google if you need any help!)

package com.equalize.groovy.testing

import com.sap.gateway.ip.core.customdev.processor.MessageImpl
import com.sap.gateway.ip.core.customdev.util.Message

// Load Groovy Script
GroovyShell shell = new GroovyShell()
def script = shell.parse(new File("src/com/equalize/groovy/testing/script1.groovy"))

// Initialize message with body, header and property
Message msg = new MessageImpl()
msg.setBody(new String("Hello Groovy World"))
msg.setHeader("oldHeader", "MyGroovyHeader")
msg.setProperty("oldProperty", "MyGroovyProperty")

// Execute script
script.processData(msg)

// Display results of script in console
println("Body:\r\n" + msg.getBody())

def displayMaps = { String mapName, Map map ->
	println mapName
	map.each { key, value -> println( key + " = " + value) }
}
displayMaps("Headers:", msg.getHeaders())
displayMaps("Properties:", msg.getProperties())

 

You will now notice some syntax errors in both scripts, the most noticeable one would be for the following elusive class:-

com.sap.gateway.ip.core.customdev.util.Message

Now this is where the fun really begins. Did you bother to read Vadim’s blog? You will need it now. Go hunt for the required JAR file, then add it to the project’s build path.

Once you got all the syntax error sorted, try executing the testing script:-

Right Click > Run As > Groovy Script

Oh no! More errors – looks like some other JAR files are required during runtime. So keep doing the below:-

Hunt, Add, Test, Repeat

Finally, when all is in place, you will be rewarded with the below screen in the console output 🙂

 

Voila! Now, this is how you test your Groovy scripts!

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. sapbinary key

    Nice workaround. much needed one. This reminds me stand alone java mapping testing.

    I am not yet into HCI . Had a little chance to play with HCI long back.

     

    message mapping test is available in HCI. It would be nice if SAP does the same with scripts also. and of course changing the colorless editor too.

     

     

    (2) 

Leave a Reply