• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a quick way to test a servlet?

 
Xavi Villalta
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Xavi Villalta
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends.

Is there a reason you must test them in a container?
 
Xavi Villalta
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

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