my dog learned polymorphism*
The moose likes Testing and the fly likes test case for servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "test case for servlets" Watch "test case for servlets" New topic
Author

test case for servlets

Swastik Pal
Greenhorn

Joined: Nov 21, 2011
Posts: 3
How to create test cases for servlets? Should I use JUNIT or I have to use CACTUS test suite??
Please give an example
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Can you elaborate a little bit on what the servlet does or are you just asking in general?

Depending on what the servlet renders, how complex of a servlet you want to test or how complex you want the tests to be will drive what is best for you.

Stand alone JUnit may not be the best as you'd have to deal with opening the port, reading responses, etc yourself. I'd suggest you look at HtmlUnit if you are working with some simple servlets. If you are doing something a little more complex then take a look at Selenium.

Note that I am assuming you are referring to a servlet rendering HTML.
Swastik Pal
Greenhorn

Joined: Nov 21, 2011
Posts: 3
Actually I'm using a servlet to transfer control from a HTML page to another servlet. How to generate test cases for this controller servlet? Will JUnit be helpful in this case?
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Sounds like you should look at something like HtmlUnit or Selenium. I believe HtmlUnit is an extention of JUnit, while depending on which portion of Selenium you use it is more of less a JUnit extention as well (thinking the older RC or Remote Control) or can be using within JUnit (the newer Selenium Webdriver support).

I have used all three. Realize that none of these options, Junit included, is going to "test" your application for you. They are all just frameworks underwhich you need to understand what to test and then write the code to do it. (Not trying to talk down to you, just trying to clarify what you expect JUnit or Cactus to do for you.)

I suggest you start with HtmlUnit and write up a test to see if it can do what you want. In my opinion it is pretty simple to use, but I used it after I understood JUnit concepts.

If you use Selenium, the Webdriver option seems better overall but is relatively new so you may find you need to spend a little more time understanding it and even writing some of your own support classes.

Selenium RC is deprecated but may offer you a little more support information -- I recommend you avoid this one as it is deprecated and as such you should not expect enhancements from it. I have refactored a number of tests to use WebDriver that were RC and I think WebDriver is a much more solid, less error prone approach. The refactoring of the framework of the tests themselves was very simple, creating helpers to do what RC did out of the box is what slowed me down.

There are examples of all three on the web. Selenium as some good "hello world" examples in its documentaion. Google "junit test servlet", Selenium, etc and you should be on track to finding examples with which to start working.
 
 
subject: test case for servlets
 
Similar Threads
Unit testing tools
need suggestion for hosting my web-app
Some General Questions.
Can I prepare for this exam w/WindowsME?
Update text area values without page reload