Setting up a classpath only for junit test.

M

michau

Hi,
I'm testing a method which uses the following statement:
myObject.getClass().getClassLoader().getResourceAsStream("resource-
name1").
I have set a classpath element entry (in .classpath file for my
project), that specifies the location of this resource so that the
resource is found correctly. However for the purpose of testing I'm
using a different name of the resource (resource-name2) and I want to
place that resource in one of my folders allocated only for testing.

I know I could add another classpath entry to my .classpath file, that
would specify the location of resource-name2, but that would make it
visible to the whole project, whereas I would want that setting to be
only within "the scope" of junit tests. So do people know if it's
possible to somehow specify a classpath entry in my junit tests, or in
some junit properties?

Thanks for any help,
Michal.
 
L

Lothar Kimmeringer

michau said:
I know I could add another classpath entry to my .classpath file, that
would specify the location of resource-name2, but that would make it
visible to the whole project, whereas I would want that setting to be
only within "the scope" of junit tests. So do people know if it's
possible to somehow specify a classpath entry in my junit tests, or in
some junit properties?

Assuming you're talking about Eclipse, you can open the Run...-
dialog, select the JUnit-test from the list of classes that
can be executed and change to the "Classpath"-pane. There you
can add additional entries to the list and change the order,
i.e. put that new entry to the head of the list.

If you're not talking about Eclipse I'm sure there is something
similar possible.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
M

markspace

michau said:
Hi,
I'm testing a method which uses the following statement:
myObject.getClass().getClassLoader().getResourceAsStream("resource-
name1").

When testing resource loading, sometimes I just create the resource on
the fly in my tests, and pass the name of the resource in. E.g.:


String classname = getClass().getSimpleName();
String rsrcName = classname.replace( ".", "/" ).concat( ".class");
URL rsrc = getClass().getResource( rsrcName );
if( "file".equals( rsrc.getProtocol() ) ) {
File f = new File( rsrc.getFile() );
String btest = "b-test";
File bfile = new File( f.getParent(), btest );
bfile.createNewFile();
String expResult = "";
String result = StringUtils.readUtf8Resource( getClass(),
btest );
assertEquals( expResult, result );
.... more tests
} else {
fail( "Could not create file to test with." );
}

Here I'm putting the resource in the unit test directory (getClass() is
being called in the context of the unit test itself). But you could do
something like this to add test files to a different .class tree too.
 
T

Tom Anderson

I'm testing a method which uses the following statement:
myObject.getClass().getClassLoader().getResourceAsStream("resource-
name1").
I have set a classpath element entry (in .classpath file for my
project), that specifies the location of this resource so that the
resource is found correctly. However for the purpose of testing I'm
using a different name of the resource (resource-name2) and I want to
place that resource in one of my folders allocated only for testing.

I know I could add another classpath entry to my .classpath file, that
would specify the location of resource-name2, but that would make it
visible to the whole project, whereas I would want that setting to be
only within "the scope" of junit tests. So do people know if it's
possible to somehow specify a classpath entry in my junit tests, or in
some junit properties?

You could put the tests in a different project. One project for the real
code, another for the tests and associated resources. Other projects
reference the real code project, not the test one. We do this quite a bit
where i work, and it's nice because it keeps things separated, and so
simplifies packaging up a project for release, gives you somewhere you can
dump test-related bits and bobs without feeling guilty, etc. It does mean
having two projects rather than one, though, and is a bit of a faff to set
up if you already have a single project working nicely.

tom
 
R

Roedy Green

Hi,
I'm testing a method which uses the following statement:
myObject.getClass().getClassLoader().getResourceAsStream("resource-
name1").
I have set a classpath element entry (in .classpath file for my
project), that specifies the location of this resource so that the
resource is found correctly. However for the purpose of testing I'm
using a different name of the resource (resource-name2) and I want to
place that resource in one of my folders allocated only for testing.

I know I could add another classpath entry to my .classpath file, that
would specify the location of resource-name2, but that would make it
visible to the whole project, whereas I would want that setting to be
only within "the scope" of junit tests. So do people know if it's
possible to somehow specify a classpath entry in my junit tests, or in
some junit properties?

Thanks for any help,
Michal.

It might be simpler and more portable just to configure a static final
resource name or a testing boolean. Then you know for sure which
resource you are getting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top