Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
engswee
Active Contributor

[Update 11 Mar 2022]


This post was written more than 4 years ago. Technology moves at the light of speed, and as such, some of what is described here is out of date, but I don't have the time to adjust it continuously to be always in sync with the latest. If you are interested in developing and testing your Groovy scripts, I'd suggest you check my E-Bite on this topic instead which is more current and should provide you a more comprehensive approach to get started. https://blogs.sap.com/2020/06/15/learn-to-develop-groovy-scripts-for-sap-cpi-with-our-new-sap-press-...


 

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 7a519509aed84a2c9e6f627841825b5a.

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!

 

Update 20 Oct 2017

Interesting observation - if you head over to (https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/062f7a7e5c374e67b568dddc7b7...), there is a recently added section detailing how you can add the JAR file for the Message class into your Groovy project 🙂



Another interesting observation is that it does not tell you where to download the file! 😛

 

Update 5 Jan 2018

Further interesting observation. The online documentation has been updated with details on how to download the JAR file for the above mentioned elusive class, now termed the Script API Methods.



 

Basically, you just have to get it from the SAP Development Tools page



 

The problem with this approach is that it only gives you a real bare bones JAR file.



 

IMHO, if you want to do any serious development/testing work locally in Eclipse, this JAR file is insufficient and not worth the effort.
47 Comments
Labels in this area