Donnerstag, 23. Mai 2013

Jnario on Guice

I love writing code in Xtend,  most of you have noticed that already. But there is another language that I see as a perfect partner for Xtend: Jnario. If you don't know it yet, go check it out!

Jnario is a DSL for writing short and readable unit and acceptance tests. The expression syntax is the same as Xtend, so there is not much learning involved. What the Jnario team has added are the concepts of Suites, Features (given-when-then acceptance tests) and Specifications (fact based unit tests). And since it compiles down to plain old JUnit, you can use all your existing test utilities. I introduced it at my current project this monday and people are actually writing tests for fun!

One requirement I almost always have for more involved test cases is dependency injection. In the past I've mainly been using Guice for that. Luckily, Jnario allows you to control how tests are created and so implementing a DI mechanism was straightforward. The source is available on Github and I am currently working on a Maven repository.

All you have to do is to annotate your tests with "@CreateWith(typeof(GuiceSpecCreator))". If you want to add some bindings, you can also add the "@InjectWith" annotation, specifying the Guice module to use for building the Injector. The following code snippet shows how Jnario's contexts and dependency injection are a perfect fit to write tests for different environments. It is actually a test from the jnario-guice integration itself: