Skip to content

When unit testing is not enough

June 19, 2008 by Przemysław Bielicki

I'd like to share my experiences about unit testing (using JUnit) Java servlets outside of the servlet container. Agile world tells us that we should automate as much tests as we can - it would be good if all aspects of the developed system are completely tested. We should test functional as well as non-functional requirements of our systems. But can all tests be automated? What is the REAL value of such tests? The reality brings us problems and pitfalls even experienced developers fall into. I will present such story regarding automated and manual testing of Java servlets.

The problem
Quite recently I had do develop some "proxy" servlets facilitating Ajax request from web browser to our middleware layer. We couldn't directly send Ajax requests because JavaScript security model doesn't allow to request data from other address than it was originally downloaded (similar restriction to Java Applet).

All right then, our servlets were not doing any amazing job (some input and output transformations were needed, however) but I made some bugs even having 100% test code coverage (used Cobertura 1.9).

What about automated tests?
Well some things can be automated, some not. Let's start with unit testing servlets. The simplest (containerless) solution to unit test servlets is to use EasyMock in order to provide mocks for HttpServletRequest, HttpServletResponse, etc. Yes, I know you can use Jakarta Cactus but I did it quicker using easy mock :)

OK - I unit tested my servlets and was very happy that I saw the green light. I started the sevlet container accessed the web page and it seemed to be working. Unfortunately I didn't see that the servlet set some instance variables after the first invocation and these variables couldn't be changed afterwards. The effect was disastrous - servlet was sending the same request all the times not taking into account passed parameters. We could have checked this problem by invoking my doXXX method multiple times with different parameters - we didn't.

Did we test our servlets manually?
Yes, non-automated tests were performed using JMeter. I used JMeter to check how our servlets behave under heavy load. The performance and load results were OK but I noticed in the logs many entries similar to this one:

Checkout date [2008-10-15] is earlier than or equals checkin date [2008-10-11]

Well the log message lies! And the implementation is well-coded (really). What's wrong?
This is how I discovered problem with SimpleDateFormat - not to mention the help from FindBugs that showed me big red light in the affected lines :)

What was wrong?

  1. I forgot that using instance variables is not thread-safe (there is only one servlet instance created by the container)
  2. I forgot that java.text.SimpleDateFormat has synchronization problems and cannot be used as a static class field
  3. I took UTF-8 for granted - unfortunately default encoding was set to the server's default one (this problem was not described above but it appeared during further manual UI testing)

Lesson learned
When you want to test your servlets:

  1. You should invoke all methods you test multiple times
  2. You should test your servlets using couple of simultaneous threads to see whether there is no shared data clash
  3. Be sure you set required content type and content encoding before you flush any response - DON'T take anything for granted
  4. You should use FindBugs to check any common Java problems (e.g. SimpleDateFormat) - either using Eclipse/NetBeans plugin or Maven report
  5. ALWAYS test your servlets under heavy load - what works for single user can stop working when many users try to use your application

Using load testing tools you can discover quite interesting and unexpected problems - without such test you cannot be sure your application will work under every circumstances.

After implementing all fixes our servlets seem to work very well even under heavy load. We don't have any thread-safety issues and our dates are parsed correctly ;)

Conclusion
To conclude shortly I will say that testing servlets is not easy. You can write unit test, "inject" mock request, response, etc. objects but you still will not be sure how your system behaves inside servlet container under heavy load (you should also try using Jakarta Cactus framework).
Agilists say that you should automate as much tests as you can. I generally agree with it but sometimes it makes more sense to perform some manual testing which simply works. I probably would not discover all the issues I revealed using only automated tests.

If you have other experiences with testing Java servlets, please share them here.

About the Author: Przemysław graduated from Gdańsk University of Technology in 2004 having specialized in Distributed Information Systems. He worked in Lufthansa Systems, Intel Corporation in the past where he developed complex IT solutions in many Java-related technologies. In professional life he is a real Java expert holding couple of Sun Java certificates (Programmer, Developer, Web Developer) and Certified Scrum Master, of course.

Przemysław is a regular contributor to AgileSoftwareDevelopment.com and the author of "From Java to Java EE" blog. He now works as a Software Craftsman in an international company that is the leading Global Distribution System (GDS) and the biggest processor of travel bookings in the world. Contact Przemysław

Comments

About SimpleDateFormat

June 19, 2008 by Riccardo (not verified), 2 years 10 weeks ago
Comment id: 1593

Nice you pointed to a very subtle issue with SimpleDateFormat :-)
It is strange how 99% of the people I worked with simply skipped the part in the docs where it explicitly says that instances for that class are not threadsafe (or they don't know what thread safety is about).
They usually implement it as a static field in some utility class without any kind of synchronization; when I stumple upon such terrible mistake I usually try to teach the writer about the fact that similar code WILL cause problems and you know what's the answer I get (always the same one!)? "we never had problems with that"!
next time I find myself in that situation I will try to link them this blog entry, hoping they believe at something that's wriitten.
Thanks

About SimpleDateFormat

June 19, 2008 by pbielicki, 2 years 10 weeks ago
Comment id: 1594

Like I wrote in my post there is a very simple and agile solution - use FindBugs - you will see this issue as an ERROR. It's enough for me :)

Findbugs and similar

June 19, 2008 by Riccardo (not verified), 2 years 10 weeks ago
Comment id: 1595

People I'm talking about almost don't know the existence of findbugs/pmd and such; simply they won't use them because "they give too many errors"!

Findbugs and similar

June 20, 2008 by pbielicki, 2 years 10 weeks ago
Comment id: 1596

OK - that's the real problem. But, if you use some continuous integration framework (Continuum, Bamboo, etc.) with Maven2 or even Ant script you could easily (at least using Maven2) add the plugin that will be automatically doing the check and sending emails in case of errors or even warnings (depending on the threshold you set)

But anyway - changing the way people think and work is the biggest problem and challenge...

There is a trick to use when introducing findbugs and similar

June 23, 2008 by Sergey Pashin (not verified), 2 years 10 weeks ago
Comment id: 1597

Exactly, that is the one of the most popular reasons. The trick is this: First just let the Continuous Integration system run findbugs, pmd an alike and show the team the trend of defects found by static analysis such as one provided by our Parabuild. Once the team got used to the fact that there is a system that automatically detects defects, you can begin looking at the most dangerous ones and fixing them, one by one. The trend would go down nicely. When all issues are fixed, it is now possible to ask the team members to run static analysis before checking in new changes and to turn on failing build when any of the issues pop up.

Sergey

uggs on sale

August 26, 2010 by uggs on sale2010, 1 week 15 hours ago
Comment id: 9440


Even celebrities love wearing boots for their comfortable cheap uggs for sale features. You too wanna try out this sheepskin footwear for your family? Well, it’s a great idea. With Sheepskin cheap ugg boots being made in plenty of styles and sizes, you can choose a pair for all members of your family from UGGS On Sale.
Such asUGG Bailey Button|UGG Argyle Knit|UGG Cardy|UGG Crochet|UGG Flower|UGG Leopard|UGG Mini|UGG Paisley|UGG Short|UGG Tall|UGG Elsey|UGG Infants Erin|UGG Langley|UGG Lo Pro|UGG Locarno|UGG Mayfaire|UGG Nightfall|UGG Rainier Eskimo|UGG Sundance II|UGG Tall Stripe Cable Knit|UGG Ultimate Bind|UGG Ultra Short|UGG Ultra Tall|UGG Suede|UGG Upside|UGG Roxy|UGG Seline|UGG Corinth|UGG Liberty|UGG Highkoo|UGG Knightsbridge|UGG Bomber Jacket|UGG Adirondack|UGG Suburb Crochet|UGG Adirodack II|UGG Broome|UGG Gissella|UGG Felicity|UGG Bailey Button Boots|UGG Classic Argyle Knit Boots|UGG Classic Cardy Boots|UGG Classic Crochet Boots|UGG Classic Flower boots|UGG Classic Leopard Boots|UGG Classic Mini Boots|UGG Classic Paisley Boots|UGG Classic Short Boots|UGG Classic Tall Boots|UGG Elsey wedge Boots|UGG Infant's Erin Baby Boots|UGG Langley Boots|UGG Lo Pro Button Boots|UGG Locarno Boots|UGG Mayfaire boots|UGG Nightfall Boots|UGG Rainier Eskimo Boots|UGG Sundance II Boots|UGG Ultimate Bind Boots|UGG Ultra Short Boots|UGG Ultra Tall Boots|UGG Suede Boots|UGG upside Boots|UGG Roxy Tall Boots|UGG seline Boots|UGG Corinth wedge Boots|UGG Liberty Boots|UGG Highkoo Boots|UGG Knightsbridge Boots|UGG Bomber Jacket Boots|UGG Adirondack Tall Boot|UGG Suburb Crochet Boots|UGG Adirodack boots II|UGG Broome boots |UGG Gissella boots |UGG Felicity boots|ugg swell|ugg desoto|ugg espresso|ugg cove|ugg ashur|ugg gaviota|ugg brookfield|ugg smithfield|ugg shoreline|ugg tess|ugg stella|ugg brookfield tall|ugg roseberry|ugg kensington|ugg chrystie|ugg bailey fancy|ugg sandra|ugg tasmina|ugg payton|UGG Handbags|UGG Swell Tall Boots|UGG Desoto Espresso Boots|UGG Cove Boots|UGG Ashur Boots|UGG Gaviota Boots|UGG Brookfield Boots|UGG Smithfield Boots|UGG Shoreline Boots|UGG Tess Boots|UGG Women's Classic Tall Boots|UGG Women's Stella Boots|UGG Brookfield Tall Boots|UGG Roseberry Boots|UGG Kensington Boots|UGG Women's Classic Short Boots|UGG Chrystie Boots|UGG Bailey Button Fancy|UGG Sandra Boots|UGG Tasmina Ultramarine|UGG Payton Boots|UGG Classic Tall Boots 5885|Kid's UGG Boots 5281|Kid's Classic Short 5251|UGG Bailey Button Triplet 1873 |UGG Men's Classic Short |UGG Kids Classic Tall Boots 5229|Kid's UGG Bailey Button Boots

ugg boots sale

September 1, 2010 by ugg boots sale, 1 day 14 hours ago
Comment id: 11184

1.Ugg footwear is a great regard for your base like many reasons.cheap uggs for sale. Probably the most rocky landscape. Another equally important disputeugg uk is the ever-mounting medley of styles ugg boots saleand ensign that you are removable to buy ugg boots, actually form to the shape of the arch.UGG Bailey Button|UGG Argyle Knit|UGG Cardy|UGG Crochet|UGG Flower|UGG Leopard|UGG Mini|UGG Paisley|UGG Short|UGG Tall|UGG Elsey Wedge|UGG Infants Erin|UGG Langley|UGG Lo Pro|UGG Lo Pro Button|UGG Locarno|UGG Mayfaire|UGG Nightfall|UGG Rainier Eskimo|UGG Sundance II|UGG Ultimate Bind|UGG Ultra Short|UGG Ultra Tall|UGG Suede|UGG Upside|UGG Roxy|UGG Seline|UGG Corinth Wedge|UGG Liberty|UGG Highkoo|UGG Knightsbridge|UGG Bomber Jacket|UGG Adirondack|UGG Suburb Crochet|UGG Bailey Button Boots|UGG Classic Argyle Knit|UGG Classic Cardy Boots|UGG Classic Crochet Boots|UGG Classic Flower boots|UGG Classic Tall Boots 5684 Leopard|UGG Classic Mini Boots|UGG Classic Paisley Boots|UGG Classic Short Boots|UGG Classic Tall Boots|UGG Elsey wedge Boots|UGG Infant's Erin Baby|UGG Women's Langley Boots Black|UGG Lo Pro Button Boots|UGG Women's Locarno Boots|UGG Mayfaire boots|UGG Nightfall Boots|UGG Rainier Eskimo Boots|UGG Sundance II Boots|UGG Ultimate Bind Boots|UGG Ultra Short Boots|UGG Ultra Tall Boots|UGG Suede Boots|UGG upside Boots|UGG Roxy Tall Boots|UGG seline Boots|UGG Women's Corinth Boots in Cocoa|UGG Liberty Boots|UGG Highkoo Boots|UGG Knightsbridge Boots|UGG Bomber Jacket Boots|UGG Adirondack Tall Boot|UGG Suburb Crochet Boots|UGG Swell|UGG Desoto Espresso|UGG Cove|UGG Ashur|UGG Gaviota|UGG Shoreline|UGG Smithfield|UGG Tess|UGG Classic Tall|UGG Women's Stella|UGG Brookfield|UGG Roseberry|UGG Kensington|UGG Chrystie|UGG Sandra|UGG Tasmina Ultramarine|UGG Payton|UGG Felicity|UGG Gissella|Ugg Stripe Cable Knit|UGG Broome|UGG Adirondack|UGG Swell Tall Boots|UGG Desoto Espresso Boots|UGG Cove Boots|UGG Ashur Boots|UGG Gaviota Boots|UGG Shoreline Boots|UGG Smithfield Boots|UGG Tess Boots|UGG Classic Tall Boots II Boots|UGG Women's Stella Boots|UGG Brookfield Boots|UGG Roseberry Boots|UGG Kensington Boots|UGG Chrystie Boots|UGG Handbags|UGG Sandra Boots|UGG Tasmina Ultramarine Boots|UGG Payton Boots|UGG Felicity Boots|UGG Gissella Boots|Ugg Stripe Cable Knit Tall Boots|UGG Broome Boots|UGG Adirondack Boots II|UGG Bailey Button Fancy|Kid's UGG Boots 5281|UGG Bailey Button Triplet|UGG Classic Tall 5885|Kid's Classic Short 5251|UGG Men Classic Short|UGG Kids Classic Tall 52292.timberland boots3.mbt shoes4.tiffany5.ugg uk

Snow boots

September 1, 2010 by Snow boots, 1 day 10 hours ago
Comment id: 11606

Our website-- 2ugg is making a great promotion now. There are many preferential ugg boots . At present , hot sale ugg classic boots such as:
Snow Boots
| UGGs On Sale
| UGG Australia Boots
| UGG Classic Boots
| UGG Classic Short Boots II
| UGG Gissella Boots
| UGG Payton Boots
| UGG Sandra Boots
| UGG Bailey Button Triplet Boots
| UGG Kensington Boots
| UGG Classic Tall Stripe Cable Knit Boots
| UGG Felicity Boots
| UGG Classic Tall Boots 5885
| UGG Chrystie Boots
| UGG Broome Boots
| UGG Bailey Button Fancy Boots
| UGG Adirondack Boots II
| UGG Kid's Boots
| UGG Kid's Bailey Button Boots
| UGG Kid's Classic Tall Boots
| UGG Kid's Classic Short Boots
| UGG Men 's Classic Short Boots
| UGG Gaviota Boots
| UGG Swell Tall Boots
| UGG Roseberry Boots
| UGG Brookfield Tall Boots
| UGG Stella Boots
| UGG Caroline Boots
| UGG High-Heel Tall Boots
| UGG Tess Boots
| UGG Desoto Boots
| UGG Brookfield Boots
| UGG Smithfield Boots
| UGG Shoreline Boots
| UGG Ashur Boots
| UGG Cove Boots
| UGG Men's Brookfield Boots
| UGG Bailey Button Boots
| UGG Argyle Knit Boots
| UGG Cardy Boots
| UGG Crochet Boots
| UGG Flower Boots
| UGG Mini Boots
| UGG Paisley Boots
| UGG Short Boots
| UGG Tall Boots
| UGG Elsey Wedge Boots
| UGG Infants Erin Boots
| UGG Langley Boots
| UGG Lo Pro Boots
| UGG Locarno Boots
| UGG Mayfaire Boots
| UGG Nightfall Boots
| UGG Rainier Eskimo Boots
| UGG Sundance II Boots
| UGG Ultimate Bind Boots
| UGG Ultra Short Boots
| UGG Ultra Tall Boots
| UGG Suede Boots
| UGG Upside Boots
| UGG Roxy Boots
| UGG Seline Boots
| UGG Corinth Wedge Boots
| UGG Liberty Boots
| UGG Highkoo Boots
| UGG Knightsbridge Boots
| UGG Bomber Jacket Boots
| UGG Adirondack Boots
| UGG Suburb Crochet Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots
| UGG Size Guide
| UGG Boots
| UGGs On Sale
| Cheap ugg boots
| UGG Bailey Button Boots
| UGG Classic Argyle Knit Boots
| UGG Classic Cardy Boots
| UGG Classic Crochet Boots
| UGG Classic Flower boots
| UGG Classic Tall Boots 5684 Leopard
| UGG Elsey wedge Boots
| UGG Elsey wedge boots Black
| UGG Infant's Erin Baby Boots
| UGG Langley Boots Black
| UGG Lo Pro Button Boots
| UGG Locarno Boots
| UGG Mayfaire boots
| UGG Mayfaire boots black
| UGG Mayfaire boots Sand
| UGG Nightfall Boots
| UGG Rainier Eskimo Boots
| UGG Sundance II Boots
| UGG Ultimate Bind Boots
| UGG Ultra Short Boots
| UGG Suede Boots
| UGG upside Boots
| UGG upside Boots black
| UGG Roxy Tall Boots
| Ugg Roxy Boots black
| Ugg Roxy Boots Sand
| UGG seline Boots
| UGG seline Boots black
| UGG Women's Corinth Boots in Cocoa
| UGG Liberty Boots
| UGG Highkoo Boots
| UGG Knightsbridge Boots
| UGG Knightsbridge Boots black
| UGG Bomber Jacket Boots
| UGG Adirondack Tall Boot
| UGG Suburb Crochet Boots
| Tiffany
| Tiffany Necklaces
| Tiffany Jewellery
| Timberland boots
| mbt shoes
| ugg boots sale
| ugg uk
| NBA Shoes
| Puma Shoes
| MBT Shoes
Please believe us , we will use passionate service attitude to receive you , until you buy satisfied goods.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.

Best of AgileSoftwareDevelopment.com