Tag Archives: Unittest

Dynamic Groovy Pt. 1

Certainly you know that Groovy is one of the dynamic languages published in the last years. There are some languages with this approach: Ruby, JRuby, Python, PHP, JavaScript and many more.

Groovy is a little bit special if you are a Java-Developer. Groovy is built on the top of the Java-Platform. So it is easy to learn the concepts and syntax of this nice language. Aside of powerful syntax enhancements there are the dynamic approach. With the Meta Object Protocol (MOP) you are able to analyze and change every Object.

Getting all properties of an Object

Properties in Groovy are a little bit special. In difference to fields a property always have a Getter-/Setter-Method. It is very easy to get all of such properties:

def someObject = new SomeJavaObject()
    println it.name

With this codefragment you get a list of all properties of the Object someObject. The content of this list are groovy.lang.MetaBeanProperty. With this class you can

  • get and set the field
  • get and set the Getter and Setter separately
  • query for a special property

You can see in Picture 1 the Classdiagram of the MetabeanProperty class. With the help of this class you can easily analyze and change existing Groovy classes.


Picture 1: Classdiagram of groovy.lang.MetaBeanProperty

What is a MetaClass?

Every Class has some meta informations. This are informations about fields, properties and methods. Additionally it exists an invokeMethod. This method is used to call dynamically methods of Groovy Classes. The behaviour of this method is similar to the Reflection mechnism of Java (you know the package java.lang.reflect?).


Picture 2: Classstructure of groovy.lang.MetaClass

Dynamically adding method to classes

With the help of this class you are able to add new methods to an existing class. Even final (and immutable) classes could be extended:

String.metaClass.toFirstUpper << {
	delegate[0].toUpperCase() + delegate.substring(1)

This little fragment adds one method to the final class java.lang.String. After this code is executed you can access this from your¬† Groovy Code. Please attend that this only works if you are calling the additional methods from Groovy. You cannot call it from your Java Code, because the Compiler doesn’t know anything of this additonally method. But you can use reflection with a simple invokeMethod to use the dynamically added method.

In Groovy you can use the new method directly:

def myString = "thisIsASimpleString"
assert "ThisIsASimpleString" == myString.toFirstUpper()

If you use Java you need to use Reflection

String myString = "thisIsASimpleString";
Method m = String.class.getMethod("toFirstUpper", new Class[]{});
assert "ThisIsASimpleString" == m.invokeMethod(myString, new Object[]{});

Adding static methods

This works for static methods, too.

public class Customer{
   String firstName
   String lastName

Customer.metaClass.static.create << { String first, String last ->
    new Customer(firstName: first, lastName: last)

assert "Thorsten" == Customer.create("Thorsten", "Kamann").firstNam


This was Part 1 of Dynamic Groovy. In the next part we see how to add dynamically add constructors, properties, and adding methods to interfaces.


Webtests Reloaded – Webtests with Selenium, TestNG, Groovy and Maven

Testdriven Development is a must-have if you want to have high quality in your project. The most difficult part you can test is the WebUI. There are many tools to support you in testing UIs. But mostly they are expensive and difficult to use. But there is a light…

Selenium is started to simplify the test of WebUI. But Selenium as standalone is not fully automated. This talk describes how to automate the test of WebUI.

Interesting links:

Vortragsreihe Dortmund 09.02.2009: Webtests reloaded – Webtests mit Selenium, TestNG, Groovy und Maven

Testgetriebene Entwicklung ist ein Muss, um die Qualität von Software-Produkten zu sichern. Der wohl am schwierigsten zu testenden Teil einer Anwendung ist die Weboberfläche. Oftmals ist es so, dass Oberflächen lediglich manuell getestet werden – mit allen Nachteilen, die manuelle Test mit sich bringen. Das Gespann Selenium, TestNG, Groovy und Maven bietet Ihnen einen Lösungsansatz, mit dem Sie in vertretbarer Zeit automatisierte Tests für Weboberflächen erstellen können. Dieser Vortrag führt Sie anhand einer Webanwendung Schritt für Schritt durch den Prozess, sodass Sie danach mit eigenen Experimenten beginnen können.