« Preaching to the Inconvertible? | Main | Dell Technical Support: Fact or Fantasy? »
September 24, 2004
ServletTestHelper - you're my hero!
Largely as a result of a coincidence, my project has had a couple of bugs reported recently which have been caused by people placing redirection logic in servlets without using a return statement after the response.sendRedirect(...) call. Remembering to include the return can be a tricky thing to do: conceptually a redirect changes the flow of control within the application, but from a Java perspective, any code after the redirect call in the method will still be executed, just like in any other method call. Depending on what this subsequent code does, the symptoms of this problem can be somewhat difficult to diagnose.
Mock object testing to the rescue!
Also concidentally, I've been using the Mock Object ServletTestHelper for the first time in anger today, and discovered a rather pleasant side-effect of this mechanism for servlet testing. If you forget to include a return statement and another two redirect calls happen to be executed within the same test, you get an error a along the lines of:
junit.framework.AssertionFailedError: MockHttpServletResponse.sendRedirect had different sizes
Expected Size:1
Received size: 2 ...
Better still, this exception is thrown not from an explicit call to verify() or a redirect-specific assertion, but from the central testDoPost() method, so you don't even need to add extra checks to your tests to ensure this rule is followed. You get it for free!
Posted by Andy Marks at September 24, 2004 12:33 PM
Comments
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.)