Re: [Httpunit-develop] Accessing web.xml from u nit tests? getResourceAsStream…

Russell Gold <[email protected]>
Newsgroups gmane.comp.web.httpunit.devel
Message-ID <[email protected]>
On Apr 14, 2006, at 6:59 PM, Gordon Henriksen 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  
> servletunit. I was being really deeply and exceedingly lazy and using:
>
>     new ServletRunner(new File("./src/webapp/web.xml"), "/context");
>
> This smacks of ‘the wrong way to do it’ to me, but it was  
> sufficient for the task at hand.

If there is a better way, it might be to define a system property  
that gives you the base path. That would work no matter where you run  
from.

>
> 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:
>
>     new ServletRunner(Thread.getContextClassLoader 
> ().getResourceAsStream("/WEB-INF/web.xml"), "/context");

That shouldn't work since the path /WEB-INF is probably not in your  
class path. You need something that is in your class path and that  
has a known relationship to the location of your WEB-INF directory.   
If you don't want to use the system property route, if you tell me  
your directory and classpath setup, I may be able to suggest an  
alternative.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.