Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading up a web.xml for integration tests with jetty

Tags:

java

jvm

jetty

OK this is kind of related to : 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).

like image 728
Michael Neale Avatar asked Apr 30 '09 03:04

Michael Neale


1 Answers

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).

like image 93
Adam Batkin Avatar answered Nov 12 '22 02:11

Adam Batkin