bharal
bharal

Reputation: 16174

java - Change classloader for a given class

So all the resource reading in this project is done like so:

SomeLoadingClass.class.getResource(resource);

For a test, I'd like to hook up a different config file for use. Just as a safety net that the config file has the right stuff in it.

Given that I'm writing a test in test class A, and the above code snippet happens somewhere else, how can I - Add a new file to the classpath (presumably for the class SomeLoadingClass) so that the config file I want is used, not the one that is set in the .classpath file.

I'd prefer to be able to "overwrite" the existing config file, instead of blowing away the entire contents of the .classpath - is that possible? If so, how?

Apologies - I've read a few of the classpath entries here but they don't seem to do quite what I want. (They're all about overwriting the current thread's classloader, which doesn't seem appropriate here - i.e. does not work)

EDIT for this one test i would like to shimmy with the classpath. That's it, nothing more. I don't want to change every other test. Also, no maven is being used.

Upvotes: 2

Views: 103

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

If you are using maven, i believe you could put your test config file in the src/test/resources dir with the same name and it will override the real config file at test time.

Upvotes: 1

Related Questions