wood burning stoves*
The moose likes Testing and the fly likes Is there a quick way to test a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Is there a quick way to test a servlet?" Watch "Is there a quick way to test a servlet?" New topic
Author

Is there a quick way to test a servlet?

Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
I was wondering if there was any way of quickly and easily testing a servlet and its functions instead of having to copy all the files to tomcat and then adding a reference in the server.xml
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Depends on what you're trying to test. Ideally there should be very little logic in a servlet other than marshalling data to/from the view layer.

You can mock objects using a mock framework, or use mock implementations. Several frameworks provide implementations of HttpServletRequest/etc. to use while testing web components outside a container.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Or you can run your webapp as an exploded application - meaning you add a context.xml in TOMCAT/conf/Catalina/localhost/ pointing to your development location - but then you need to have the right webapp structure in your IDE (or what you are using)

You can read more about the context.xml here (the link is for tomcat 5.5, but is also valid for v6 and v7): http://tomcat.apache.org/tomcat-5.5-doc/config/context.html


Regards, Rene Larsen
Dropbox Invite
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Rene Larsen wrote:Or you can run your webapp as an exploded application - meaning you add a context.xml in TOMCAT/conf/Catalina/localhost/ pointing to your development location - but then you need to have the right webapp structure in your IDE (or what you are using)

You can read more about the context.xml here (the link is for tomcat 5.5, but is also valid for v6 and v7): http://tomcat.apache.org/tomcat-5.5-doc/config/context.html


I read somewhere that usually you have to restart tomcat for changes to take effect. Would that be true in this case?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Depends.

Is there a reason you must test them in a container?
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
Actually I'm having trouble with Tomcat right now (getting the servlet and applet to actually start) so I'll just focus on that for now until I can get everything to work.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Sometimes when you just change a method body the containner will do a "Hot Replace".


But if you create a new method or change methods parameters, the containner might not work. (I cant tell you why now)



[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Xavi Villalta
Greenhorn

Joined: Aug 02, 2010
Posts: 29
The thing is I didn't change the code at all, it just stopped showing up all of a sudden.
I was having the same problem with applets before too, just plain old stand-alone applets, they wouldn't show up in the browser either even though they displayed correctly in the applet viewer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a quick way to test a servlet?