*
The moose likes Servlets and the fly likes help me in writing a unit test case for the given servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "help me in writing a unit test case for the given servlet" Watch "help me in writing a unit test case for the given servlet" New topic
Author

help me in writing a unit test case for the given servlet

suresh kumar dontamsetti
Greenhorn

Joined: Apr 01, 2006
Posts: 8
Hi guys/gals,

Below is the code for my Servlet :

public MyServlet extends HttpServlet{

public void init(ServletConfig config){super.init();};
private void doGet(HttpServletRequest req,HttpServletResponse res){

Private static Connection getConnection(){
Connection con = null;
//here is the code for the connection and returns Connection object
return con;
}
public ArrayList getRecords(){
ArrayList arl = new ArrayList();
//here is my code and returns ArrayList
return arl;
}
public HashTable getdata(){
HashTable ht = new HashTable();
//here is my code and this returns a hashtable
return ht;
}
public void getmetadata1(){
//here is the code and this returns nothing
}
}
public void destroy(){};
}


Please give me the cod e for the test case for the above code.

and now if i am calling any other servlets function from this servlet,
do i need to write different unit test cases for each servlet or include the test methods in the same test case that i'm writing for my First Servlet.


cheers
Suresh Kumar
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41076
    
  43
What exactly do you mean by "the test case"? What do you want to test?

Also, it looks like you have methods inside the deGet method, so this wouldn't even compile.

As an aside, it's a bit harder to unit-test servlets, because you need a testing environment like Cactus or JUnitEE.


Ping & DNS - my free Android networking tools app
suresh kumar dontamsetti
Greenhorn

Joined: Apr 01, 2006
Posts: 8
Hi,
than q for the suggestion.
but i had the JUNIT to test the test cases.
the thing i wanted to know from the given code is, if i had these many methods in a single servlet and i'm calling all of them from the doGEt() or doPsot() and when i have these many methods with different return types how would i write a test case?
please tell me how to write the test cases for the given servlet with methods returning different return types.
cheers
Suresh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41076
    
  43
You still don't say what you want to test. The servlet as part of a web application (so that you would need to simulate a web client calling it)? Or just the methods inside the servlet? If the former, then a library like HttpUnit or HtmlUnit (both on SourceForge) can help you. If the latter, you possibly could test them outside of a web container, just like any JUnit test.

If you're unsure about JUnit testing itself, then the JUnit documentation would be a good start.
suresh kumar dontamsetti
Greenhorn

Joined: Apr 01, 2006
Posts: 8
hi,
what i exactly want to know is?
if i'm writing different methods in a servlet and calling the methods of any other servlet from the same servlet.
then i need to write a single test case for all the methods that i'm calling from the servlet or write different test cases for the different servlets.

cheers
Suresh Kumar
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41076
    
  43
One doesn't call servlet methods from a different servlet. Maybe you somehow manage to do this, but you most definitely should not, so the question of how to unit-test this is for all practical purposes moot.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You might delegate all the work to a plain old java object that has no dependencies on J2EE classes like request, response and session.

Now you can test WorkerBee without any complicated objects like request and response. With some effort you can even make WorkerBee independent of databases and other "model" classes so you can test it in perfect isolation.

The code in the servlet that gets parameters and such off the request goes untested here. You might choose to trust it or use one of the more complex tools to build up request & session to test it. It would be interesting to build a single servlet that handles all requests so you only have to test it once.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
suresh kumar dontamsetti
Greenhorn

Joined: Apr 01, 2006
Posts: 8
Hi,
Thanks man.

cheers
Suresh
 
Consider Paul's rocket mass heater.
 
subject: help me in writing a unit test case for the given servlet
 
Similar Threads
Connection Pool
Connection Busy with another result hstmt Error - updated (SOLVED WITH JTDS)
Threads access to object and performance/object in ServletContext: value or reference
How to write a class use Hashtable to add, update, delete an Object....?
Jstl not reflecting values back to servlet