---
title: "How to integrate Concordion in Play Framework and write acceptance tests in natural language"
date: 2013-07-05T08:32:54+00:00
author: "poornerd"
tags: ["bdd", "Concordion", "howto", "PlayFramework", "tdd", "Testing", "Unit Testing"]
canonical: https://www.poornerd.com/2013/07/05/how-to-integrate-concordion-in-play-framework-and-write-acceptance-tests-in-natural-language/
source: Raw Markdown twin of the HTML article; content is the original source.
---
[<img class="alignleft size-medium wp-image-382" alt="Screen Shot 2013-07-05 at 10.24.25" src="https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.24.25-300x249.png" width="300" height="249" srcset="https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.24.25-300x249.png 300w, https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.24.25.png 807w" sizes="(max-width: 300px) 100vw, 300px" />](https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.24.25.png)After starting to read <a href="http://specificationbyexample.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/specificationbyexample.com');" target="_blank">&#8220;Specification by Example&#8221;</a>, I wanted to have a go a writing acceptance tests for my Play Framework projects.  I stumbled onto Concordion (<a href="http://www.concordion.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.concordion.org');" target="_blank">http://www.concordion.org/</a>), because it is similar to <a href="http://cukes.info/" onclick="javascript:pageTracker._trackPageview('/outbound/article/cukes.info');" target="_blank">Cucumber</a> but written in and for Java.

> Concordion is similar to Cucumber but focuses on readability.

Concordion integrates as Unit tests (which is the primary focus of this post), and lets you write the Tests in Natural Language in HTML Files so that it also becomes self documenting.

I got most of how to integrate Concordion from Craig Aspinall in Australia (<a href="http://twitter.com/aspinall" onclick="javascript:pageTracker._trackPageview('/outbound/article/twitter.com');" target="_blank">@aspinall</a>) who had written a Blog post about it, but which was no long &#8220;online&#8221;.   (he sent me a link to the markdown so I could try it out &#8211; thank Craig!)

### Integrating Concordion

You first need to add the dependency to your `Build.scala`, and then add a line so that the SBT copies the Concordion HTML files:

<pre class="brush: plain; title: ; notranslate" title="">import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

    val appName         = "computer-database"
    val appVersion      = "1.0"

    val appDependencies = Seq(
       "org.concordion" % "concordion" % "1.4.2" % "test"  ,
      javaCore,
      javaJdbc,
      javaEbean
    )

    val main = play.Project(appName, appVersion, appDependencies).settings(
      // Next, you need to instruct SBT to copy the Concordion HTML files to the target folder
        unmanagedClasspath in Test &lt;+= (baseDirectory) map { bd =&gt; Attributed.blank(bd / "test") }
    )
}

</pre>

### Telling Play where to output the Test results to

This turned out to be a challenge. If you do not set the `concordion.output.dir` system property, then they are outputed to your Java temp directory. Craig suggested setting it in the `Build.scala`, but as it turns out the most recent Version of Play forks the Tests into a separate process and loses the setting. So I ended up adding this to my Global Settings class:

<pre class="brush: plain; title: ; notranslate" title="">import play.Application;
import play.GlobalSettings;
public class Global extends GlobalSettings {

    public void onStart(play.Application arg0) {
        if (arg0.isTest())
            System.setProperty("concordion.output.dir", "target/test-reports/concordion");
    }

}
</pre>

 _Note: I think I discovered another Bug in Play, because although this works, if I run &#8220;test-only&#8221; it doesn&#8217;t!!!_

### The Test

The key to getting Play to recognize and run Unit Tests is the @Test annotation, so it seems that the easiest way to get Play to run the Concordion tests is to add a method like this to your Concordion fixture class:

<pre class="brush: plain; title: ; notranslate" title="">@Test
    public void runThisTest() {
    }
</pre>

I ended up implementing one of the tutorial examples from here: <a href="http://www.concordion.org/Tutorial.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.concordion.org');" target="_blank">http://www.concordion.org/Tutorial.html</a>

You write the test case in HTML, and use <span> tags with Concordion elements to specify the paramters and assertions:

<pre class="html">&lt;html <b>xmlns:concordion="http://www.concordion.org/2007/concordion"</b>&gt;

    &lt;head&gt;
        &lt;link href="../concordion.css" rel="stylesheet" type="text/css" /&gt;
    &lt;/head&gt;

    &lt;body&gt;

        &lt;h1&gt;Splitting Names&lt;/h1&gt;

        &lt;p&gt;
            To help personalise our mailshots we want to have the first name
            and last name of the customer. Unfortunately the customer data
            that we are supplied only contains full names.
        &lt;/p&gt;

        &lt;p&gt;
            The system therefore attempts to break a supplied full name into
            its constituents by splitting around whitespace.
        &lt;/p&gt;

        &lt;div class="example"&gt;

            &lt;h3&gt;Example&lt;/h3&gt;

            &lt;p&gt;
                The full name
                &lt;span <b>concordion:execute="#result = split(#TEXT)"</b>&gt;John Smith&lt;/span&gt;
                will be broken into first name
                &lt;span <b>concordion:assertEquals="#result.firstName"</b>&gt;John&lt;/span&gt;
                and last name
                &lt;span <b>concordion:assertEquals="#result.lastName"</b>&gt;Smith&lt;/span&gt;.
            &lt;/p&gt;

        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>

Then you write a fixture class with the Tests (and the `@Test` &#8220;hack&#8221; I mentioned).

<pre class="brush: plain; title: ; notranslate" title="">import org.junit.Test;
import play.mvc.Result;

import java.util.HashMap;
import java.util.Map;

import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.*;

import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;
import org.junit.Test;

@RunWith(ConcordionRunner.class)
public class ConcordianTestFixture {

    public Result split(String fullName) {
        Result result = new Result();
        String[] words = fullName.split(" ");
        result.firstName = words[0];
        result.lastName = words[1];
        return result;
    }

    class Result {
        public String firstName;
        public String lastName;
    }

    @Test
    public void runThisTest() {
    }
}
</pre>

When you run the tests, then the resulting HTML page it outputed (as previously defined) to your `target/test-reports directory`. Here is an example of a failed test (for a successful one, see the image at the start of the post):

[<img class="alignleft size-full wp-image-387" alt="Screen Shot 2013-07-05 at 10.30.39" src="https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.30.39.png" width="807" height="672" srcset="https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.30.39.png 807w, https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.30.39-300x249.png 300w" sizes="(max-width: 807px) 100vw, 807px" />](https://www.poornerd.com/wp-content/uploads/2013/07/Screen-Shot-2013-07-05-at-10.30.39.png)

I hope you try this out!  Let me know if you have more ideas or tips&#8230;
