This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Testing and the fly likes how to test servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "how to test servlets" Watch "how to test servlets" New topic
Author

how to test servlets

ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Hi,
I am given an assignment on testing servlet.I have gone through the net and after hours of surfing found nothing useful.i am not able to configure the cactus framework in the eclipse .If somebody has code relating this please do forward it to me.please let me know if we can test servlets without using the cactus framework? if yes please send me a sample servlet testcase with explanation.
thanks
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You don't necessarily need Cactus for testing Servlets. After all, Servlets are just plain old Java classes that happen to extend from HttpServlet. What I usually do for Servlets is good old unit tests that throw mock request and response objects at the Servlet and verify that the Servlet 1) makes the appropriate calls to whatever service layer components you have, and 2) forwards the request to/renders the expected view.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
can you please send me a sample testcase to test the servlet.I am in great hurry.thanks a lot for helping me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42033
    
  64
Another approach I sometimes use if I want to test not just the servlet, but also that the resulting HTML page is correct (which mostly is created using JSP) is to use jWebUnit.


Ping & DNS - my free Android networking tools app
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
Thank you for your prompt reply..

Could you please send me a sample code.. so that I can quick learn...!!
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I don't have sample code at hand - sorry. There's a whole chapter in my book about writing unit tests for Java web components, though...

If you're in a hurry, you could take the approach in this article and apply that to Servlets (rather than Spring's components they talk about in the article).
ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
sir,
can you tell me which book you were refering to so that i can go through the book for my reference.Thanks a lot for your help sir.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42033
    
  64
The book "Test Driven" is linked in Lasse's signature.
 
GeeCON Prague 2014
 
subject: how to test servlets