Skip to content

My blog

Elite Dangerous, SoapUI and other tech stuff!

  • About
  • Elite Dangerous
    • Elite Dangerous Mining Guide
    • My Asp Explorer
    • My Cobra MKIII
    • My Diamondback Explorer
    • My Hauler
    • My Type-6
    • My Type-7
    • My Viper MKIV
    • My Vulture
  • SoapUI Cookbook Bonus Recipes
  • SoapUI Plugins

R2-Using SoapUI Groovy Grape dependencies

Posted on October 11, 2016 by admin

Scenario

If you want to use a Groovy or Java library that isn’t bundled with SoapUI, then the standard way is to add it to /bin/ext and restart SoapUI, as per Recipe R1. A slick alternative approach is to use the Groovy Grape dependency manager to add Maven repository artefacts as part of your Groovy Scripts on-the-fly!

Why bother?

There’s no compelling reason to use Grape over the standard way to include dependencies. However a different approach can sometimes present its own opportunities, some differences and possible advantages are:

  • Grape dependencies can be added at runtime without a restart.
  • Grape driven scripts are self contained and document their own dependencies.
  • Grape dependencies are managed and centralised by Groovy rather than SoapUI (see the More section).
  • Ease of use – there’s no need to manually download or package Grape dependencies.

Prep

Recipe Test Version Info:
SoapUI 5.2.1 (standard distribution / installer)
O/S: Windows 10
Java: Bundled JRE

Other: ivy-2.1.0.jar,ivy-2.4.0.jar,jsoup-1.9.2.jar,jsoup-1.10.1.jar

Project Setup
To try this out, we just need somewhere to run a Groovy script within SoapUI e.g. a Groovy TestStep.

Steps

1.Create A Groovy script with unresolved dependencies
Create a script that needs a dependency that SoapUI doesn’t already have in its /lib folder. In this example, I picked a script that requires libraries from the HTML parser JSoup. The script just parses an HTML string and extracts the anchor tag and displays it in a popup (once we satisfy all its dependencies).

[code language=”groovy”]
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element

def html = "<p>An <a href=’http://rupertanderson.com/’><b>example</b></a> link.</p>"
Document doc = Jsoup.parse(html)
Element link = doc.select("a").first()

log.info link
[/code]

2. (Optional) Test that the dependency is initially unresolved
As a test to make sure that the dependency doesn’t already exist within SoapUI’s classpath, run the above script to verify that the required imports are not resolved. You should see a SoapUI Error popup containing something like:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script2.groovy: 3: unable to resolve class org.jsoup.nodes.Document @ line 3, column 1. import org.jsoup.nodes.Document ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class….

3. Add Ivy jar
Unfortunately we can’t use Grape without first adding the Ivy jar. At time of writing the latest version of ivy is ivy-2.4.0.jar, this can be obtained from:

https://mvnrepository.com/artifact/org.apache.ivy/ivy/2.4.0

For classpath reasons this needs to be added to /lib (not /bin/ext/) otherwise you will see the error:

SoapUI missing Ivy dependency

3. Add a Grape statement

To get the script working, we need to use Grapes to include the JSoup dependency. Again, Maven Central can help us, as it provides a useful Grapes tab with the syntax we need:

  • Go to https://mvnrepository.com/artifact/org.jsoup/jsoup/1.10.1 (or possibly later version of JSoup)
  • Click the Grapes tab
  • We can extract just the @Grab part of the @Grapes directive i.e.

[code language=”groovy” highlight=”3″]
// https://mvnrepository.com/artifact/org.jsoup/jsoup
@Grapes(
@Grab(group=’org.jsoup’, module=’jsoup’, version=’1.10.1′)
)
[/code]

Then paste this in above the imports like so:

[code language=”groovy” highlight=”1″]
@Grab(group=’org.jsoup’, module=’jsoup’, version=’1.10.1′)
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element

def html = "<p>An <a href=’http://rupertanderson.com/’><b>example</b></a> link.</p>"
Document doc = Jsoup.parse(html)
Element link = doc.select("a").first()

log.info link
[/code]

4.Run the script successfully!
Run again, and you should see the anchor tag from within the html variable extracted and output in the log:

[code language=”groovy”]
<a href="http://rupertanderson.com/"><b>example</b></a>
[/code]

More

Local Repository

If you were wondering where SoapUI stores the locally cached artefacts when they are downloaded, the default location is:

[code language=”bash”]
${user.home}/.groovy/grape
[/code]

If you have Groovy installed, then typing the following in a shell:

[code language=”bash”]
grape list
[/code]

Will give you a list of all artefacts cached in the above location.

For configuration options please see:
http://docs.groovy-lang.org/latest/html/documentation/grape.html#Grape-Advancedconfiguration

Feature Request

If you like using the Grape functionality and would like to see Grape dependency management added as part of the standard SoapUI product, then maybe vote for this feature request:

https://community.smartbear.com/t5/SoapUI-Feature-Requests/Adding-Groovy-Grape-dependency-management-out-of-the-box/idi-p/130390

Links

  • For more JSoup examples see https://jsoup.org/cookbook/
Posted in SoapUI Cookbook Bonus RecipesTagged Grab, SoapUI Groovy Grape

Post navigation

Issue/Fix: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
Replace Request In REST TestSteps SoapUI Plugin

2 Comments

  1. Rao says:
    November 8, 2016 at 3:53 am

    Hi Rupert,

    This is nice article. Will try once I get chance.
    Hope many people out there in the community would benefit out of it.

    Reply
  2. Rup says:
    November 8, 2016 at 9:20 am

    Hi Rao,

    Thanks, I value any feedback you can give me on it.

    If you get a chance to try it out, please can you double check whether adding the ivy jar to /bin/ext/ also works? I found that it didn’t during my tests (which kindof made sense, because that /ext lib classloader is secondary to the SoapUI /lib classpath where Groovy exists), but I have seen others suggest that it does work. It wouldn’t be a big deal, but I want to be as accurate as I can! 🙂

    Thanks,
    Rup

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 5500Lyr Passenger Site Seeing Mission
  • Mining pristine rings in an unpopulated anarchy
  • Replace Request In REST TestSteps SoapUI Plugin
  • R2-Using SoapUI Groovy Grape dependencies
  • Issue/Fix: SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”

Recent Comments

  • Krish on R1-Developing and using custom Groovy libraries In SoapUI
  • Rup on R2-Using SoapUI Groovy Grape dependencies
  • Rao on R2-Using SoapUI Groovy Grape dependencies
  • Rup on R1-Developing and using custom Groovy libraries In SoapUI
  • Karel on R1-Developing and using custom Groovy libraries In SoapUI

Archives

  • May 2017
  • March 2017
  • December 2016
  • October 2016
  • April 2016
  • March 2016

Categories

  • Elite Dangerous
  • SoapUI Cookbook Bonus Recipes
  • SoapUI Plugins
  • SoapUI Setup

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Proudly powered by WordPress | Theme: micro, developed by DevriX.