OpenEJB and some BDD

BDD is a fine way to test its code. Personally i like the way it allows to link code expert with domain experts. Here i’ll try to show how to do it with OpenEJB using two interesting BDD frameworks.

The well known Spock

Spock is known because it is used on tapestry (at least that’s the way i discovered it ;)). Here is its website

Using spock is pretty easy since you simply have to use its arquillian integration and OpenEJB arquillian adapter to make it running in a JEE container.

Spock is often used with groovy…you see any issue? No!

First write your code as usually (let take an hello world). Then add to your pom some code to compile groovy in java (for instance):


Then add dependencies needed (ziplock is just used in the test as an utility method and is not mandatory):

  <version>1.8.0</version> <!-- spock doesn't work with 2.0.0-rc-4 -->


Note: groovy is not in scope test because with the compiler configuration it can be used for the main code too.

Before going further one optional task: configuring Specification to be used instead of Test as suffix for tests classes by the surefire plugin to follow the spock convention:

    <includes> <!-- we could have used * as by default but it is more consistent -->

Now the pom should be fine so let’s write our test in groovy (yeah!) using the arquillian-spock integration and OpenEJB as runtime:

import org.apache.ziplock.JarLocation
import org.jboss.arquillian.container.test.api.Deployment
import org.jboss.shrinkwrap.api.ArchivePaths
import org.jboss.shrinkwrap.api.ShrinkWrap
import org.jboss.shrinkwrap.api.asset.EmptyAsset
import org.jboss.shrinkwrap.api.spec.WebArchive
import spock.lang.Specification

import javax.inject.Inject

import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNotNull

class HelloSpecification extends Specification {
    private Hello hello

    def static WebArchive "create archive"() {
            .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))

    def "Hello.hi() method should return 'hi'"() {
            assertNotNull hello

            assertEquals "hi", hello.hi()

Cucumber-jvm: the rails ruby ported to JVM languages

Cucumber is a famous testing framework in Rails world and was recently ported to JVM languages.

An OpenEJB integration is proposed.

Note: it has a little constraint which is to import log4j (was the case for old openejb version but now it should be optional – an issue was opened for it).

The idea is pretty simple: write a Gherkin feature file (the extension is feature too) and then create a kind of suite runner which will scan its package to find step defs.

Here is a runner:

import cucumber.junit.Cucumber;
import org.junit.runner.RunWith;

public class CucumberTest {
    // the suite, will load automatically the features

Here a a feature file:

Feature: Hello

  Scenario: Say Hello
  Given A name 'foo'
  Then The bean says 'hello foo'

And here the corresponding steps:

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;

import javax.inject.Inject;

import static org.junit.Assert.assertEquals;

public class HelloStepDef {
    private Hello hello;

    private String name;

    @Given("^A name '([a-z]*)'$")
    public void initName(final String name) { = name;

    @Then("^The bean says '([ a-z]*)'$")
    public void checkResult(final String result) {
        assertEquals(result, hello.hello(name));

Note: several languagaes are available for cucumber annotations (simply switch of package to find them).

So the job is done! we can match values (even multiple ones) from the feature file then get them in the test.

It is pretty simple and efficient.

The current drawback i see is each method is counted as a test by surefire where i could have expected that a scenario is a test but well that’s a detail.

A funny and nice thing (even if it is not as nice as in Ruby) is if you forget some steps the code to write will be printed in the console :). So generally we write features, run the test and copy paste as a starting point for steps.


BDD is really possible today even with JEE so why wouldn’t use it?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s