Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

test case for servlets

 
Swastik Pal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic