File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
suresh kumar dontamsetti
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
suresh kumar dontamsetti
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
suresh kumar dontamsetti
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks man.

cheers
Suresh
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic