| Web testing BDD-style with JWebUnit and Easyb |
|
|
Behaviour-driven development is a great way to design and build the web layers of your application. In this article, I look at how to use JWebUnit, a fast and light-weight web testing framework, with Easyb, a powerful Groovy-based BDD framework. JWebUnit is a web testing framework built on HTMLUnit. It runs in memory, so it's fast. However, it also has an intuitive, high-level API, making it easy to use even if you have only an approximate idea of the exact HTML details of your application screens. A simple JWebUnit test case is shown here:
JWebUnit works fine like this, but we can still do better by adding a little BDD-flavoring to the mix. As it turns out, behaviour-driven development (or "behaviour-driven design") is a great way to model user interaction with your application. In BDD, you structure your requirements in a "given [some condition or state], when [some event occurs] then [we expect something else to happen]". For example, in an online banking application, one such requirement could be "Given that I am logged onto the bank web site and my bank account balance is $0, when I deposit $100, then the new balance should be $100." Easyb is a behaviour-driven development (BDD) framework for Java and Groovy applications. It is based on Groovy, but uses a special DSL to express requirements and tests in BDD-style. Like many BDD frameworks, easyb lets you express your requirements (more precisely, your user's behaviour) in almost plain English, before filling in the details with test code. For example, here is a working Easyb test scenario describing how we might interact with our banking application:
As far as Easyb is concerned, this is executable test code - however it won't do very much. When you run Easyb against a scenario like this one, it will be flagged as "pending implementation". So now let's see how we can implement this test using JWebUnit. JWebUnit test cases need to extend the
We also need to create a new instance of this class for each test case. We can do this using the Easyb "before_each" keyword, as shown here:
Our full Easyb test story looks like this:
And running these easyb scenarios will generate you a report that looks like this:
Although this seems longer than the Java version, it is arguably more readable, and reflects the intention of the code more accurately than the Java equivalent. The test report also makes it quite clear what requirements are being tested, in very readable terms. Writing the tests using a BDD approach also helps you keep a good perspective on how closely your application actually does what your users have asked for. Reports like the one above can also give users and testers a better understanding of what is being coded. If you want to learn more about Behaviour-Driven Development, web testing, Maven, and a whole lot of other useful tools and tricks, why not come along to a Java Power Tools bootcamp session - there are workshops coming up soon in Canberra, Sydney, Brisbane and Wellington. "Probably the best training course I've been on."..."Not just how to write Java code but the 'business end' - how to build, test, deploy, manage and monitor"..."One of the best and most useful courses I have attended. And they didn't even try to sell me anything!" - There are still some places for the courses in Wellington, Canberra, Brisbane and Sydney - Get up to speed with the latest and coolest in Java tools and best practices! Sign up on the 2009 season of the Java Power Tools Bootcamps.
Bookmark
Email this
Trackback(0)
Comments (0)
![]() Write comment
|