Test Mashup, TDD / BDD
So these days test driven or behavior driven development are hot around my coordinates. Im going to write a bit about pratical usage of BDD using selenium & jBehave. With backup tools such as maven, cobertura and selenium.
We all hate to maintain large code bases of tests, tests are nice ( and necessary ) but sometimes you just get cluttered by them.
Introducing jBehave
So what if you could have a minimal amount of code and write something like this:
When user goes to homepage
homepage are rendered
Taking it a bit further:
Given a user nino with password dummy
When user goes to homepage and does a login
Then user are logged in and shown profilepage
That would be nice right? In jBehave you archive this by a blend of things, i’ll only cover a subset.
Steps
In your steps class you something like the below code, notice that I put in selenium. So the steps are sort of a mini test framework which the scenarios can utilize.
@When("a user visits $page")
public void visits(String page) {
selenium.open("http://localhost:8080/" + page);
}
@Then("$page are rendered")
public void seesPage(String page) {
Ensure.ensureThat(selenium.getLocation().compareToIgnoreCase(
"http://localhost:8080/" + page) == 0);
Ensure.ensureThat(!selenium.isTextPresent("HTTP ERROR: 404"));
}
Scenario
The scenario are a plain text test, which utilizes the steps above. Something like this:
When a user visits homepage
Then home are rendered
When a user visits errorpage
Then errorpage are rendered
As you can see you could actually put in use cases, and things suddenly got a lot easier to discuss with customers, your tests are readable by non technical eyes and of course it’s not limited to web development. And your maintenance code base got smaller since what you need to write in java have shrunken, you are only building a small framework for the scenarios to utilize. Really really cool!
As you can see jBehave fit’s both for unit testing and the more obvious integration testing.
Introducing Cobertura
While you test it’s still nice to know your code coverage, cobertura’s nice for this as it gives you simple readable reports and it’s transparent to use. Just include a small mojo in your parent pom:
In your parent pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>
net.sourceforge.cobertura.datafile
</name>
<value>
target/cobertura/cobertura.ser
</value>
</property>
</systemProperties>
</configuration>
</plugin>
And in the project pom using it
<plugins>
<reporting>
…
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<formats>
<format>html</format>
</formats>
</configuration>
</plugin>
…
</plugins>
</reporting>
</project>
You will then get reports looking like this:

Cobertura report
And you can even see which lines are touched in your code:

See what lines are touched by tests
Introducing Selenium
Automatic independent browser tests, if your doing web development this is almost a must have, unless you like to do manual tests a lot. However if you are using code coverage which are using byte code instrumentation, you could loose track of what’s tested and what not. A way around this are perhaps to start a embedded jetty server as part of your test, then in theory you should be back on track. Since byte code instrumentation should be able to track again…