You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Gordon Henriksen <go...@mac.com> on 2006/04/15 00:56:21 UTC

Accessing web.xml from unit tests? getResourceAsStream…

We're using Eclipse and Maven. I've got some unit tests for a web  
application (war packaging), and I'd like to pull in web.xml for an  
in-memory servlet container (servletunit). I was being really deeply  
and exceedingly lazy and using:

     new File("./src/webapp/web.xml");

This smacks of ‘the wrong way to do it’ to me, but it was sufficient  
for the task at hand.

And it worked fine under Eclipse, but then I went to mvn test before  
committing; to my disappointment, Maven runs the unit tests relative  
to the parent pom, more often than not, so I can't use a relative  
path. After some research I tried this instead:

     Thread.getContextClassLoader().getResourceAsStream("/WEB-INF/ 
web.xml");

That gets me no love, though. Am I using the wrong path? I've tried  
several different methods of getting class loaders, and several  
different path names, but to no avail. Is anyone else doing this? And  
please forgive any naiveté regarding Java resource handling. :)

In the meantime, I'm hardcoding the servlet mappings.

— G


Re: Accessing web.xml from unit tests? getResourceAsStream…

Posted by dan tran <da...@gmail.com>.
use basedir system property avaiable in any maven build

new File( basedir + "/src/webapp", web.xml)

-D


On 4/14/06, Gordon Henriksen <go...@mac.com> wrote:
>
> We're using Eclipse and Maven. I've got some unit tests for a web
> application (war packaging), and I'd like to pull in web.xml for an
> in-memory servlet container (servletunit). I was being really deeply
> and exceedingly lazy and using:
>
>     new File("./src/webapp/web.xml");
>
> This smacks of 'the wrong way to do it' to me, but it was sufficient
> for the task at hand.
>
> And it worked fine under Eclipse, but then I went to mvn test before
> committing; to my disappointment, Maven runs the unit tests relative
> to the parent pom, more often than not, so I can't use a relative
> path. After some research I tried this instead:
>
>     Thread.getContextClassLoader().getResourceAsStream("/WEB-INF/
> web.xml");
>
> That gets me no love, though. Am I using the wrong path? I've tried
> several different methods of getting class loaders, and several
> different path names, but to no avail. Is anyone else doing this? And
> please forgive any naiveté regarding Java resource handling. :)
>
> In the meantime, I'm hardcoding the servlet mappings.
>
> — G
>
>
>