After one and a half year of developing test scripts using java a
colleague in Persado convinced me to give a shot to groovy. Playing
around for couple of weeks and there you are; I was convinced that
groovy would be the new exciting thing in my testing life. Taming my
excitement I decided to strategically replace blocks of java code
with groovy. The plan was not to replace code in page objects neither
test scripts but rather code in the supporting functions in
abstraction layers between the test scripts and the page objects.
The decision in replacing the particular pieces of code was driven by the way groovy handles Lists, Arrays and Maps. In addition the effortless way of creating and using objects in a less wordy way than java showed the way.
Setting up groovy in my test environment was easy and I will show how in a feature post. With my environment ready and iron maiden in my youtube I started. First stop an object creation to carry data to a JMS dispatcher there you are.
Point 1: The list kind was never declared in the object definition.
“list.collect { it.toString()}”
Of course it is expected that there would be people asking why go with groovy while I can do the same things with java ? Yes using groovy will not add extra arrows in your quivers but it can simplify your code and save you a lot of extra lines of code. Let me give you an example.
In one of my tests I wanted to simulate the following scenario
“There are 200 people owning a cat, 300 owning a dog 140 owning a lion and 230 owning a tiger. From the people owning the cat I want 15 of them to sent a letter and likewise 63 letters from the dog owners 22 from the lion owners and 33 from the tiger owners.”
The code I had developed with java spanned in several lines
The decision in replacing the particular pieces of code was driven by the way groovy handles Lists, Arrays and Maps. In addition the effortless way of creating and using objects in a less wordy way than java showed the way.
Setting up groovy in my test environment was easy and I will show how in a feature post. With my environment ready and iron maiden in my youtube I started. First stop an object creation to carry data to a JMS dispatcher there you are.
import org.joda.time.DateTime class MyObject { String myString; List myList; Date myDate; }
kaboom
no setters no getters that simple how not to love the simplicity
especially if your are not a developer. Now
lets use the object, fill it up with data.
def rangeEvents = (11223300000 .. 11223300500).collect { it.toString()} ldt = new DateTime() def myObject = new MyObject(myList:rangeEvents,myString:"Hello",myDate:ldt);
Point 1: The list kind was never declared in the object definition.
Point 2: Assignment is as easy as
myList:rangeEvents
Point
3: Ranges are Lists and each element of
the list can be manipulated by a single statement“list.collect { it.toString()}”
Of course it is expected that there would be people asking why go with groovy while I can do the same things with java ? Yes using groovy will not add extra arrows in your quivers but it can simplify your code and save you a lot of extra lines of code. Let me give you an example.
In one of my tests I wanted to simulate the following scenario
“There are 200 people owning a cat, 300 owning a dog 140 owning a lion and 230 owning a tiger. From the people owning the cat I want 15 of them to sent a letter and likewise 63 letters from the dog owners 22 from the lion owners and 33 from the tiger owners.”
The code I had developed with java spanned in several lines
int catLetters=0; String[] catOwners = new String[15]; int dogLetters=0; String[] dogOwners = new String[63]; int lionLetters=0; String[] lionOwners = new String[22]; int tigerLetters=0; String[] tigerOwners = new String[33]; for (String data : petOwners) { if (data.getOwner().equals(“cat”) && catLetters<15) { catOwners[catLetters] = data.getOwnerName() catLetters = catLetters + 1; } if (data.getOwner().equals(“dog”) && dogLetters<63) { dogOwners[dogLetters] = data.getOwnerName() dogLetters = dogLetters + 1; } if (data.getOwner().equals(“lion”) && lionLetters<22) { lionOwners[lionLetters] = data.getOwnerName() lionLetters = lionLetters + 1; } if (data.getOwner().equals(“tiger”) && tigerLetters<33) { tigerOwners[tigetLetters] = data.getOwnerName() tigetLetters = tigerLetters + 1; } }
Now
with groovy we can create a Map with owners and expected number of
letters lettersOwnersMap
def petOwnersList = [] requestedMessageResponses.each { for(String data : petOwners){ if(lettersOwnersMap(it.key) && it.value>0){ petOwnersList.add(data.ownerName) it.value-- } } }
Point
1: With the java implementation the code is static meaning that if we
want to add a pet owner we need to add extra if statements. In the
groovy implementation we only need to add an extra map entry that
simple.
Update: In this post, check how to integrate Groovy in your tests while working with Eclipse.
Update: In this post, check how to integrate Groovy in your tests while working with Eclipse.