bdd
A Behavioural World
Or at least a behavioural twitter world,
http://abehaviouralworld.rapaul.com/ is a little project just launched which attempts to distill behaviours from twitter. Given my recent rapture in the world of BDD, the behaviours we are distilling are those that match the scenario format.
Given ... When ... Then ...
If you look carefully at the page source, you will notice a large portion of it is dedicated to an HTML comment containing the list of features and the breakdown into scenarios. To keep things easily accessible, each time I thought of a scenario I added it to the page, once completed, it was marked with [Done].
For example, starting with the most important feature:
Feature 1) In order to see how BDD syntax is being used on twitter As a BDD zealot I want to see tweets that contain 'given', 'when' & 'then', in that order.
I then broke this feature down into scenarios:
[Done] Given the search returns more than matching 10 tweets When the user views the page Then the 10 most recent tweets should be displayed ...
Each time a scenario was satisfied, I marked it with [Done]. Not all scenarios ended up being all that important, the following scenario is still pending.
Given the search returns no matching tweets When the user views the page Then instead of showing any tweets a message should be shown
In fact, if we attempt to tie this scenario back into the vision of the website (distilling behaviours in twitter), then we realise it adds no value. If no one is tweeting about behaviours, then it is highly unlike that anyone will be visiting our behaviour distillery.
There are a couple of features (3 & 4) in the source which haven’t yet been implemented.
Feature 3) In order to emphasis the given/when/then syntax As a BDD zealot I want to see give/when/then highlighted in the displayed tweets Feature 4) In order to encourage BDD syntax usage on twitter As a BDD zealot I want to be able to tweet from this page using a BDD template
If anyone would like to work on these features, or even just break down some useful scenarios, please feel free to fork the code on github, provide me with a diff or add a comment (note at the time of writing github looks to be having some caching issues on the ‘Source’ tab).
What no executable scenarios?
Oops, nope. I’m yet to get into the world of writing executable scenarios in Javascript. If someone has some knowledge in this area, please feel free to fork the github repository and start automating the scenarios from the page source.
PS: This idea was partially inspired by a tweet by soulnafein which read:
My girlfriend is taking the mickey out of me because recently I started explaining things using the Scenario format (Given When Then) :-)
BDDMockito & Eclipse
On the 23rd of July, Mockito 1.8.0 was released, you can see a full listing of changes in their release notes. One feature that took my fancy was the inclusion of BDD aliases for stubbing an API, instead of using when, the Mockito team now encourage the use of given to bring your tests inline with the BDD style. To illustrate this, here is an example taken from the BDDMockito javadoc.
Seller seller = mock(Seller.class);
Shop shop = new Shop(seller);
public void shouldBuyBread() throws Exception {
//given
given(seller.askForBread()).willReturn(new Bread());
//when
Goods goods = shop.buyBread();
//then
assertThat(goods, containBread());
}
By breaking the test into given, when & then, future maintainers of the tests will more quickly understand which parts of the test relate to setup, exercising the SUT and asserting.
(stealing from Apple’s AppStore ad) What’s great about Eclipse, is that if you want to write a foreach loop, there’s a template for that. If you want to create a unit test, there’s a template for that. Yip there’s an app template for just about anything.
To minimise keystrokes and encourage the use of this style, you can modify the existing Test template as shown below to automatically generate the BDD style comments & statically import the BDDMockito members. To modify the template, navigate to Window > Preferences > Java > Editor > Templates. Click on Test (the JUnit 4 test template) and click edit. Paste in the follow template:
public void ${testname}() throws Exception {
// given ${cursor}
${staticImport:importStatic('org.junit.Assert.*', 'org.mockito.BDDMockito.*')}
// when
// then
}
You are now ready to start writing BDDMockito style unit tests using Eclipse’s template, simply type Test, hit Ctrl+Space and you will be given the following option.

Selecting the option will generate the test method, focus first given to the test name, then to the blank line after // given.

There you have it, a simple Eclipse template to generate your BDDMockito style tests.
Search
Subscribe
Recent Posts
Tags
Archives
What I'm Doing...
- RT @jamescarr: I prefer story level specifications that setup their own inputs versus relying on crap that's already in the DB 6 hrs ago
- @alexcuesta Sounds like #qcon was great! Are you staying for https://secure.trifork.com/london-2010/freeevent/register.m?eventOID=2346 7 hrs ago
- Missed my tube stop playing Archipelago, sign of a compelling game. If only it had multiplayer #android 15 hrs ago
- More updates...
