You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Benson Margulies <bi...@basistech.com> on 2006/12/24 18:28:05 UTC

Failing to find system resouces in unit test in surefire

I have a class that calls ClassLoader.getSystemResourceAsStream to
retrieve an XML file. All is well when running the live code, and even
when running JUnit from Eclipse. From surefire, the XML file is not
found.

I sense that I'm missing something about class paths, but I can't find
anything really apposite in the documentation.

Help?


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Failing to find system resouces in unit test in surefire

Posted by Stephen Coy <st...@gmail.com>.
Is your XML file in src/test/resources?


On 25/12/2006, at 4:28 AM, Benson Margulies wrote:

> I have a class that calls ClassLoader.getSystemResourceAsStream to
> retrieve an XML file. All is well when running the live code, and even
> when running JUnit from Eclipse. From surefire, the XML file is not
> found.
>
> I sense that I'm missing something about class paths, but I can't find
> anything really apposite in the documentation.
>
> Help?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


RE: Failing to find system resouces in unit test in surefire

Posted by Benson Margulies <bi...@basistech.com>.
Yes, it turned out that this is a standard phenomena and adding
.getClass().getClassLoader().getResourceAsStream did the job.

-----Original Message-----
From: Tom Huybrechts [mailto:tom.huybrechts@gmail.com] 
Sent: Monday, December 25, 2006 10:01 AM
To: Maven Users List
Subject: Re: Failing to find system resouces in unit test in surefire

I'm not a surefire expert, this is just from taking a quick look at the
source:
However you run Surefire (forked or not, with child delegation or
not), the system classloader will not contain your test code. In
forked mode, it will only contain surefire and your testing framework.
Surefire itself will create an isolated classloader that contains your
test code and its dependencies and run your tests from that
classloader.

If you expect your code to work in anything but a basic Java
application, do not depend on the system classloader. And
unfortunately, surefire is not a basic app.

tom

On 12/25/06, Wendy Smoak <ws...@gmail.com> wrote:
> On 12/24/06, Benson Margulies <bi...@basistech.com> wrote:
>
> > I have a class that calls ClassLoader.getSystemResourceAsStream to
> > retrieve an XML file. All is well when running the live code, and
even
> > when running JUnit from Eclipse. From surefire, the XML file is not
> > found.
> >
> > I sense that I'm missing something about class paths, but I can't
find
> > anything really apposite in the documentation.
>
> I can reproduce it, but I can't explain it.
>
> According to the Surefire docs [1], tests normally run in an isolated
> classloader.
>
> However, setting childDelegation=true, which supposedly "makes tests
> run using the standard classloader delegation instead of the default
> Maven isolated classloader" didn't help matters.
>
> With src/main/resources/temp.properties, running from Surefire,
>    ClassLoader.getSystemResourceAsStream("temp.properties");
> returns null while
>
this.getClass().getClassLoader().getResourceAsStream("temp.properties");
> doesn't.
>
> In IDEA, neither one returns null (and the tests pass.)
>
> Here's a sample project if you want to play with it:
>
http://people.apache.org/~wsmoak/maven/system-resource-1.0-SNAPSHOT-src.
zip
>
> [1]
http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html
>
> --
> Wendy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Failing to find system resouces in unit test in surefire

Posted by Tom Huybrechts <to...@gmail.com>.
I'm not a surefire expert, this is just from taking a quick look at the source:
However you run Surefire (forked or not, with child delegation or
not), the system classloader will not contain your test code. In
forked mode, it will only contain surefire and your testing framework.
Surefire itself will create an isolated classloader that contains your
test code and its dependencies and run your tests from that
classloader.

If you expect your code to work in anything but a basic Java
application, do not depend on the system classloader. And
unfortunately, surefire is not a basic app.

tom

On 12/25/06, Wendy Smoak <ws...@gmail.com> wrote:
> On 12/24/06, Benson Margulies <bi...@basistech.com> wrote:
>
> > I have a class that calls ClassLoader.getSystemResourceAsStream to
> > retrieve an XML file. All is well when running the live code, and even
> > when running JUnit from Eclipse. From surefire, the XML file is not
> > found.
> >
> > I sense that I'm missing something about class paths, but I can't find
> > anything really apposite in the documentation.
>
> I can reproduce it, but I can't explain it.
>
> According to the Surefire docs [1], tests normally run in an isolated
> classloader.
>
> However, setting childDelegation=true, which supposedly "makes tests
> run using the standard classloader delegation instead of the default
> Maven isolated classloader" didn't help matters.
>
> With src/main/resources/temp.properties, running from Surefire,
>    ClassLoader.getSystemResourceAsStream("temp.properties");
> returns null while
>    this.getClass().getClassLoader().getResourceAsStream("temp.properties");
> doesn't.
>
> In IDEA, neither one returns null (and the tests pass.)
>
> Here's a sample project if you want to play with it:
> http://people.apache.org/~wsmoak/maven/system-resource-1.0-SNAPSHOT-src.zip
>
> [1] http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html
>
> --
> Wendy
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Failing to find system resouces in unit test in surefire

Posted by Wendy Smoak <ws...@gmail.com>.
On 12/24/06, Benson Margulies <bi...@basistech.com> wrote:

> I have a class that calls ClassLoader.getSystemResourceAsStream to
> retrieve an XML file. All is well when running the live code, and even
> when running JUnit from Eclipse. From surefire, the XML file is not
> found.
>
> I sense that I'm missing something about class paths, but I can't find
> anything really apposite in the documentation.

I can reproduce it, but I can't explain it.

According to the Surefire docs [1], tests normally run in an isolated
classloader.

However, setting childDelegation=true, which supposedly "makes tests
run using the standard classloader delegation instead of the default
Maven isolated classloader" didn't help matters.

With src/main/resources/temp.properties, running from Surefire,
   ClassLoader.getSystemResourceAsStream("temp.properties");
returns null while
   this.getClass().getClassLoader().getResourceAsStream("temp.properties");
doesn't.

In IDEA, neither one returns null (and the tests pass.)

Here's a sample project if you want to play with it:
http://people.apache.org/~wsmoak/maven/system-resource-1.0-SNAPSHOT-src.zip

[1] http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html

-- 
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org