Reputation: 133
I'm having trouble running my JUnit test, it fails because it is not able to find the projects resources.
My test starts a server which loads all the needed resources using an URLClassLoader. I have no problem with the resources not being found when running my project otherwise. This problem only occurs when I try to run my JUnit test.
I have tried adding the file paths as arguments in my runtime configuration for the test like this, ex:
-cp .:/path/to/the/config/file
But it makes no difference.
Help please!
Project structure:
Upvotes: 1
Views: 4298
Reputation: 328536
The convention in Maven is to have all Java test code in src/test/java
. This is to prevent accidental inclusion of test code in the final product. So I suggest you move your tests.
Since you don't show the code which you use to load the resource, I can't tell you whether there are any bugs in that. But /path/to/the/config/file
is most likely wrong; is must be /path/to/the/config
so you can say in Java getClass().getClassLoader().getResource("file")
.
But I suggest to use a command line argument to specify the config file:
main(String[] args) {
File configFile = new File(args[0]).getAbsoluteFile();
if(!configFile.exists()) throw new IllegalArgumentException("Unable to find config file: " + configFile);
...
}
That makes error handling much more simple. Also, it allows to use different config files in tests and later in production.
Upvotes: 1
Reputation: 26034
Default Maven test path is src/test/java
, so if your tests are not in that directory, you have to define your test path in pom.xml
with tag <testSourceDirectory>
, inside <build>
. You have to include maven-surefire-plugin
, too.
Upvotes: 2