Skip to main content

Using easyb with Maven

Posted by johnsmart on August 11, 2008 at 9:00 AM PDT

Easyb is a very cool way to test your Java application in BDD-style with Java. But wouldn't it be nice to be able to integrate your BDD stories into your Maven build process? The good news is, you can!

Writing a BDD-style test story with easyb is, well, easy. Groovy is a great way for Java developers to write tests quickly and efficiently, even if they don't know the finer points of the Groovy scripting language. For example, the following test story (in a file called "AccountDepositsStory.groovy") explores what should happen when you open a new bank account:

import com.wakaleo.onlinebank.domain.Account



/**

 * A simple scenario

 */

scenario "depositing money onto a new account", {

    given "a new account",{

        account = new Account()

    }



    when "an initial deposit is made", {

        amount = 100

        account.makeDeposit(amount)

    }



    then "the balance should be equal to the amount deposited", {

        account.balance.shouldBe amount

    }

}

This is easy enough, and there are many ways to execute it. You can run the script from the command line, or using the Ant task. One of my favorite techniques is to use the IntelliJ plugin, which is, indeed, very cool. However, as a Maven user by choice, what I really want to do is to integrate EasyB scripts into the Maven build process.

As it turns out, this, also, is pretty easy, thanks to the Maven Easyb Plugin. Just put all of your EasyB story scripts under the src/test/easyb directory and add a reference to the EasyB Maven plugin in your build plugins, as shown here:

<project...>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.easyb</groupId>
                <artifactId>maven-easyb-plugin</artifactId>
                <version>0.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

In this example, we set up Easyb to run during the test lifecycle phase (in other words, when you run "mvn test"). When you run your tests in Maven, EasyB will kick in and run all of the stories it can find in the src/test/easyb directory. By convention, it considers any file ending in "*Story.groovy", "*.story", "*Specification.groovy" or "*.specification" to be an easyb story. An example of the easyb plugin in action is shown here:

C:\Users\john\projects\onlinebank\onlinebank-core>mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building onlinebank-core
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
...
[INFO] [easyb:test {execution: default}]
[INFO] Using easyb dependency org.easyb:easyb:jar:0.9:compile
[INFO] Using easyb dependency commons-cli:commons-cli:jar:1.1:compile
[INFO] Using easyb dependency org.codehaus.groovy:groovy-all-minimal:jar:1.5.0:compile
     [java] Running account deposits story (AccountDepositsStory.groovy)
     [java] Scenarios run: 2, Failures: 0, Pending: 0, Time Elapsed: 0.368 sec
     [java] Running account withdrawls story (AccountWithdrawlsStory.groovy)
     [java] Scenarios run: 2, Failures: 0, Pending: 0, Time Elapsed: 0.123 sec
     [java] Running open account specification (openAccount.specification)
     [java] Specifications run: 2, Failures: 0, Pending: 0, Time Elapsed: 0.038 sec
     [java] 6 total behaviors run with no failures
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Mon Aug 11 10:09:24 NZST 2008
[INFO] Final Memory: 8M/14M
[INFO] ------------------------------------------------------------------------

On the downside, the plugin is still a little green - it doesn't integrate with the Maven site generation as yet, for example, the reporting is pretty basic, and you need to bind the plugin to the 'test' lifecycle phase yourself. However, the project is a very dynamic one, and these details should be ironed-out in no time - so stay tuned for the latest BDD developments!

You can check out a simple sample Maven 2 project using easyb here.

If you want to learn more about EasyB, I'll be giving a talk on the subject at the Java Emerging Technologies Conference 2008 (JET 2008) in Auckland on September 17, as well as talks for the Melbourne and Sydney Java Users Groups while I'm over that way for the Melbourne and Sydney Java Power Tool bootcamps. So, if you are interested, and in the area, come along!

Comments

Hi John Great blog your have here, with a lot of focus on tdd and bdd. So I'd tough I mention my bdoc project at http://bdoc.googlecode.com. It's based on TestDox, but is also supporting scenarios (and actually userstories). And theres a cool diff report, that lets you see the changes in your tests. Best Regards Per Otto (Norway)