File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Unit testing jsp pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Unit testing jsp pages" Watch "Unit testing jsp pages" New topic
Author

Unit testing jsp pages

Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30909
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: May 26, 2003
Posts: 30909
    
158

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

Joined: Jul 11, 2001
Posts: 14112
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unit testing jsp pages