10

I seen a few posts (for instance here 1) suggesting to use groovy to write integration tests for java applications in groovy programming language. I am working on an application using the following technologies: Java 7, java EE 6, POSTGRESQL

I know groovy is

  • easy to integrate with java
  • quick to write code

But is there any particular reason to use it for ITs? Adding another programming language to your codebase would not make your life hell? I think I am missing something from the picture, so I would appreciate your responses.

Community
  • 1
  • 1
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49

2 Answers2

4

I will soon start a project with exactly the same stack as yours, but Groovy will also be in production code, not only tests.

There is no problem in adding Groovy to your codebase, because it is an easy language. It'd be a different story if you were including some language which doesn't look a lot like java, say haskell or prolog. You are already including Facelets, EL, CSS, HTML and Javascript. Which part of adding another language is bad? :-)

The main points i pick up for Groovy in IT tests:

  • Easy to write assertions and mock data;
  • Conciseness in code;
  • Smooth learning curve;
  • Concise code to operate a browser automation like Geb;

Other just-to-cool-to-forget stuff in Groovy:

  • Easy to write XML/JSON (if you need test webservices, for example);
  • Static compilation, if you need;

We had test teams in my two last companies which weren't working in production code, but started writing tests in Groovy pretty quickly and enjoyed the language: no worries about types and stuff, just working tests!

Will
  • 14,348
  • 1
  • 42
  • 44
  • my concern was exactly the one you mentioned - convincing others to use groovy for writing tests. I already started seeing the benefits. You mentioned using groovy also for production code, do you mind sharing where you will use groovy? – Olimpiu POP Mar 20 '13 at 20:44
  • 1
    I'll use groovy everywhere :-). In my team, i'll make a presentation writing some java code and then showing the groovy version. With a big focus on collections and bigdecimals ;-). That usually buys everyone, save for java newbies. In my old companies it took time. Some guys took months to start writing Groovy code. – Will Mar 21 '13 at 11:52
  • Make a presentation writing some java test and then converting it to groovy, show the benefits, show HOW EASY it is to write a Groovy test. They will easily feel the productiveness. Don't impose anything, in my experience i've noticed that not everyone can shift languages fast enough, but happens with time. – Will Mar 21 '13 at 11:54
3

I think it should be read as Groovy is especially good for writing tests compared to Java. There is nothing in Groovy you cannot do in Java, but it would often take tremendous effort. Something like Spock for example, would be near unreadable in Java.

It is many small things that make it so. There are for example power asserts, that make the output of assertion errors from assert statements in Groovy very nice. The seamless integration allows not to have extra code to align the languages worlds. The DSL capabilities allow you easily to write minimal DSLs to reduce the boilerplate code. All this is there to enable you to see what you actually test and how. Because if a test fails, you have to understand that part exactly and it reduces the time you need to spend on writing tests.

All I can advise is trying it out for a bit. It then either convinces you or not. If your cases are very simple, then maybe Java is good enough for you. If the testing gets more complex, then having to understand some Groovy code could be the smaller hurdle here. Not to forget: as a Java developer you don't need to understand all too much about Groovy to be able to use it properly and write nice unit tests with it.

blackdrag
  • 6,413
  • 2
  • 26
  • 38