• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unit testing jsp pages

 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I really thanks to the members of this group who really helped me in writing and running junit testcases for bean methods.
Now i again want your help in testing jsp pages. The jsp page contains two text fields. In that page only i am calling jean mathod which makes a SELECT query to the DB. I am populating those two text fields by this select query.
So how can i go about testing jsp page?
Pls help me. Do any one know any good site which explains in details how to test jsp page?

Shrikant
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most important strategy is to move all the logic needing tests from the JSP to a more testable location - a POJO or a TagLib. Test that as usual.

If you really need to test the JSP, I'd probably go with ServletUnit: http://httpunit.sourceforge.net/doc/servletunit-intro.html

It's not exactly trivial, though, and I don't know of a good introduction, besides the chapter in the book in the sig below (unfortunately only available in german).

"JUnit in Action" contains a chapter on testing JSPs using Cactus, but that's even more complicated. "JUnit Recipes" likely contains good advice on the topic, too, but I don't have it at hand.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,
Do you have a preference over JWebUnit and ServletUnit. I haven't gotten too far with either of them, but JWebUnit seemed easier to use. And it's based on ServletUnit so the level of abstraction is higher.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Ilja,
Do you have a preference over JWebUnit and ServletUnit. I haven't gotten too far with either of them, but JWebUnit seemed easier to use. And it's based on ServletUnit so the level of abstraction is higher.


Don't confuse ServletUnit with HttpUnit!

ServletUnit (being a part of the HttpUnit distribution) is a servlet container designed for unit testing. It is running inside your test and gives you close control over the servlet environment. The WebConversation is directly linked to the container, that is it doesn't need to go over a socket.

With other words, ServletUnit gives you many of the benefits of in-container-testing, without incurring the overhead.

In the book, I've used it in concert with JWebUnit, but it can as easily be combined with the test-framework of your choice, such as HtmlUnit or XHtmlUnit, for example. I've even written a prototypical extension to http://www.jdemo.de which uses ServletUnit to render and display a JSP without the need to start a true servlet container.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34229
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Don't confuse ServletUnit with HttpUnit!

Ah! At least I have a good excuse for not reading your book
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:

Ah! At least I have a good excuse for not reading your book


I conclude that "not" is a short form of "learning the german language, so that I can start".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic