tags:

views:

932

answers:

1

OK this is kind of related to : http://stackoverflow.com/questions/728805/using-jetty-to-install-and-run-servlet-tests-programmatically

got great answers there, and have been able to load up servlets programmatically and its all made of awesome.

What I would like to do however is load up a web.xml in a test (all in the classpath) and have it run up a server (using the current classpath) - I have seen in docs how to point it to a directory to do that, but I want to work off the classpath (better for in place testing). Essentially validating my web.xml.

(its not relevant, but this app is in scala, but I have had no issue with that, everything works as advertised).

+3  A: 

It sounds like what you want to do is load a proper web application programatically, as opposed to loading individual servlets (and I think you want to do it without having a full WAR file to work from).

Server server = new Server( port );
WebAppContext root = new WebAppContext();

root.setWar("/path/to/somewhere");
root.setContextPath("/");

server.addHandler( root );
server.start();

The trick is that the /path/to/somewhere should contain a WEB-INF directory and your web.xml file should live inside there. Nothing else needs to live within that directory structure, as everything else can be automatically loaded from your classpath (though if you wanted to, you could make that a path to an actual WAR file or complete exploded WAR tree).

Adam Batkin
yes - that is what I ended up doing. and it works well.
Michael Neale
(and its fast ! great for "unit" tests which are really more then unit tests !) - thanks for updating this post BTW.
Michael Neale