Groovy 101 (I): groovyCalc with tests and Travis-CI integration

The time has arrived; I cannot delay it more… I have to learn Groovy and Grails. In Kaleidos I've been moved to a new project developed with Grails so I need to be productive (ASAP! ^_^).

But, I don't know Groovy. Even more, I don't know Java. And I think it would be useful to document my own process. That said, let's start in the beginning…

Disclaimer Almost all the documentation I found for beginners assumes the student knows Java; most tutorials start with the differences between Groovy and Java. As I don't know Java, this posts are going to be slightly different. Maybe I'll make some comparisons with Python, but I'll try to keep those comparisons as anecdotal notes.


You have a Linux operating system. You know the command line. You know git and github. You know something about programming (variables, methods, class…). You can check the specific syntax on your own.

What is Groovy

From the oficial documentation:

Installing Groovy

To manage and install Groovy components, it's recommended to use GVM tool.

$ curl -s | bash

Once you have installed gvm, you need to add the following to your .bashrc file:

[[ -s "/path/to/.gvm/bin/" ]] && source "/path/to/.gvm/bin/"

Now we can install Groovy:

$ gvm install groovy

Good! Next step.

Hello world

Now you have installed Groovy, you have some tools like goovyConsole, or groovysh. Go to a terminal and type:

$ groovysh

A groovy interpreter is open, and we're going to make our Hello, world with something like this:

aran :: ~/formacion/groovy101 » groovysh
Groovy Shell (2.3.9, JVM: 1.8.0_25)
Type ':help' or ':h' for help.
<strong>groovy:000> println "Hello, world!"
Hello, world!</strong>
===> null

WIN! Our first Groovy command ;-) Keep going!

Note: for specific issues concerning groovysh, check this page.

Now, create a file called hello.groovy and write the following:

println "Hello, world!"

And in the terminal, type:

$ groovy hello.groovy

You should have something like this:

aran :: ~/formacion/groovy101 » groovy hello.groovy
Hello, world!
aran :: ~/formacion/groovy101 »

Ou yeah! This is the beginning; next steps: the infinite, and beyond.


It's the moment to build a simple calculator. Let's create a directory with these two files:

aran :: ~/formacion/groovy101 » ls groovyCalc

In the Calc.groovy file, you have to write the actual code, while in the CalcTest.groovy file (using jUnit), you write the tests. In this repository you can find an example with code and tests. You can clone it, or write one by yourself (recommended).

To run these tests:

$ groovy CalcTest.groovy

It only displays errors; so “no news, good news” ;-) Now you can extend the calculator, or you can create a new simple project to learn the Groovy syntax. And don't miss the tests, they are also very important!

Bonus: Travis

Even if those tests are very very simple, I think it's useful to integrate them in a continous integration tool. I have tried with Travis CI, but you could use any other you feel confortable with.

If you have never used Travis, you can read this post Travis 101 (spanish) to get used. The .travis.yml file for this project would be:

language: groovy
    - 2.3.9
    - oraclejdk8
    - groovy CalcTest.groovy

Note: the jdk line is to force to use java8, instead of java7 (default). Currently, it's not supported openjdk8, only oraclejdk8.

You can check the status of this repository.

And that's all for now. From this moment, you can make some katas and exercises to learn the Groovy syntax. Next steps, we will learn something about gradle and spock. But now it is your turn to improve.

I would love to read your comments and feedback on this kind of posts :)

comments powered by Disqus