November 17, 2005Annotations and Behavior-Driven TestingI have been a long-time fan of annotations, and my devotion started with EJBGen (which was using JavaDoc annotations in 2001, imagine that!), followed by my involvement in JSR 175 and now, with TestNG. Annotations make a lot of sense for a testing framework, but once in a while, I still hear a few people wondering if they are appropriate for this particular domain. Not only do I have a few reasons to believe this, but interestingly, the latest emergence of something called Behavior-Driven Testing is making this point further. Here's why. JUnit requires your test methods to start with the word "test". When you do this, you are actually adding information to your method, attaching the "metadata" (extra information) that it's not just a Java method: it's a Java test method. This is the very definition of "metadata": data on top of existing data. But what are you really trying to achieve? You are flagging your method so that it can be found by JUnit. If you think a little bit about it, this goal doesn't have much to do with the name of your method. Here is another way of looking at it: what if you are using a framework that, says, automatically generates RMI stubs for any methods that start with "remote". How do you combine this with JUnit? It's easy to see that this approach doesn't scale very far. Recently, a practice called Behavior-Driven Testing (BDT) has received some publicity. The idea is to use a different terminology for your tests, not only for your test method names, but also in the way you perform your assertions. Here is an example:
Which can be rewritten as:
I'll save the discussion on the pros and cons of BDT for a future posting and observe that the idea of baking the "test" attribute into the method name breaks down for this approach, proving the point that the name of the method and what this method means to a particular framework are completely orthogonal ideas. Of course, the corollary of this observation is that TestNG already supports BDT :-) Posted by cedric at November 17, 2005 08:00 AM Comments
Hey, this comment isn’t about this particular post but rather just a "Hi" as I was browsing over your blog today. I was curious about your blog title, I’m getting a minor in Japanese, since otaku means home or house but should never be used in reference to ones own house. So if you meant for Otaku to mean like "your home on the web" then the word uchi, referring to ones own home, would have been the word to use. Anyways I probably didn’t interpret by you used Otaku right anyways but as I said I was just curious, keep up the bogging Ill check back sometime. Posted by: Noah at November 17, 2005 05:11 PMNoah: check http://en.wikipedia.org/wiki/Otaku Posted by: BoD at November 18, 2005 02:17 AMOtaku means geek. Posted by: at November 22, 2005 01:38 PMPost a comment
|