« I second the motion! | Main | Caution! Test in Progress »
July 11, 2004
TagTestHelper - you mock me!
I have to admit I'd already decided exactly how this post was going to end well before starting the coding to support my hypothesis. Having now finished the coding, I've completely changed my mind!
I was going to make the case for not making testing infrastructure overly complex unless it really needs it. The practical context I used is testing custom JSP tags and the difficulties in correctly creating the PageContext object necessary for a complete test. My general approach to these things was:
- extract a method out of
doStartTagto handle the processing and output required by the tag (for the sake of the argument, let's call this methodgenerate() - pass as parameters to
generate()all the values it needed to complete it's operation, incuding aWriterof some sort to hold the result - invoke
generate()directory from the unit test, supplying aStringWriterobject to hold the result - Trust that
generate()will be called appropriately fromdoStartTag()or there will be an acceptance test which will break if this is not the case.
01 public class TimestampTag extends TagSupport {
|
I haven't had to write a tag so far that's needed testing any deeper than this and I was convinced that alternative techniques (using mocks, in particular) was going to add way too much overhead to the test to make the added advantage of being able to call doStartTag directly worth the excess baggage.
Background duly dispensed with, I've included sample tests using both my naive approach and using mock objects below.
01 public class TimestampTagTest extends TestCase {
|
As you can see, there is very little difference between the size of the two mechanisms for testing the tag (testContentsNaive versus testContentsMock). I must admit the TagTestHelper class makes all this sort of stuff much, much easier, although I still contend the naive method results in a slightly easier-to-read test. Given this, I'm about to abandon my naive tag testing idiom and use the TagTestHelper from now on.
Note: using an alternative tag testing approach like that provided by the in-container Cactus framework can also produce tests of a similar quality to those allowed via mock objects however the effort required for configuration is substantially greater. That said, there is probably a place for some form of in-container testing in all J2EE projects.
Posted by Andy Marks at July 11, 2004 06:08 PM
Comments
Where is the TagTestHelper class from?
Posted by: Marty Andrews at July 12, 2004 08:15 AM
Marty,
TagTestHelper is one of the very useful helper classes available from the Mock Objects J2EE jar.
Posted by: Andy Marks at July 12, 2004 10:58 AM
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)