CukeSpace or BDD, Arquillian and Java 8


Cucumber supports since few days a Java 8 API.

If you didn’t see it yet here what it looks like (taken from cucumber-java8 tests):

import cucumber.api.Scenario;
import cucumber.api.java8.En;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;

public class LambdaStepdefs implements En {
    private static LambdaStepdefs lastInstance;

    public LambdaStepdefs() {
        Before((Scenario scenario) -> {
            assertNotSame(this, lastInstance);
            lastInstance = this;
        });

        Given("I have (\\d+) cukes in my (.*)", (Integer cukes, String what) -> {
            assertEquals(42, cukes.intValue());
            assertEquals("belly", what);
        });
    }
}

This is great but does it work with Arquillian?

As a quick reminder “Cuke In Space!” project integrates cucumber-jvm with Arquillian.

A Java test looks like:

@RunWith(CukeSpace.class)
public class ClientFeatureLoaderTest
{
    @Deployment(testable = false)
    public static Archive<?> createDeployment() {
        return create(WebArchive.class)
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
            .addClass(Person.class);
    }

    @Inject
    private Person person;

    private String said;

    @When("^I say \"(.*?)\"$")
    public void speak(final String word) {
        said = person.say(word);
    }

    @Given("^I have a person$")
    public void init() {
        person = new Person();
    }

    @Then("^I should have said \"(.*?)\"$")
    public void check(final String word) {
        assertEquals(word, said);
    }
}

Of course one of the strength of Arquillian is its “server” integration, this also means if you need to rely on the constructor to define steps it is quite fragile.

To solve it cukespace introduces the API Lambda:

package cucumber.runtime.arquillian.api;

public interface Lambda {
    void define();
}

Idea is to simply define your steps in this define() method.

So globally a Java 8 cukespace test would look like:

@RunWith(CukeSpace.class)
public class J8Test implements En, Lambda {
    @Deployment
    public static Archive<?> war() {
        return ShrinkWrap.create(WebArchive.class)
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
                .addClass(Person.class);
    }

    @Inject
    private Person person;

    @Override
    public void define() {
        Given("I have a person", () -> assertNotNull(person));
        When("I say \"(.*)\"", person::say);
        Then("I should have said \"(.*)\"", (String hi) -> assertEquals(hi, person.getValue()));
    }
}

This little interface finally allows to integrate cucumber-jvm and Java 8 quite smoothly and to keep container integration (CDI, EJB…) which makes you enter a new world in term of testing!

Advertisements

2 thoughts on “CukeSpace or BDD, Arquillian and Java 8

  1. Pingback: Tests BDD Cucumber simplifiés avec les lambdas Java8 | #DEV/FR Damien.FREMONT

  2. Pingback: Migrating from Simple JUnit Test to Cucumber Java | #DEV/FR Damien.FREMONT

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s