Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make maven use test resources

when I use the command: mvn test, maven uses the main resources instead of the test ones in src/test/resources.

How can i make maven use the test resources rather than the main resources?

EDIT: I use Classloader to find my resources. Classloader can find resources from my src/test/resources directory, but it looks in src/main/java first for the resource.

ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream file =loader.getResourceAsStream("resource.xml");

Or is there any property I can use while running a maven command that will prioritize a classpath?

EDIT 2: I have figured out the problem, it does load from test/resources for the test, however my test involves deploying a webapp (integration testing) and from there when the webapp accesses the resource it uses src/main/resources. Is there a way to change the order of the classpath or make one precede the other for a WAR deployed in a jetty container?

like image 345
Sujen Avatar asked Jun 07 '11 15:06

Sujen


People also ask

What is a resource in Maven?

A resource is a file in the class path folder structure for your project. This is important because your test resources will be put in your test-classes folder hierarchy and your main resources will be put in your classes folder hierarchy - both in your target folder. Maven has a standard directory layout.

How do I run a test file in Maven?

By default, Maven adds a source file App.java and a test file AppTest.java in its default directory structure, as discussed in the previous chapter. Let's open the command console, go the C:\MVN\consumerBanking directory and execute the following mvn command.

How do I build a Java project in Maven?

By default, Maven adds a source file App.java and a test file AppTest.java in its default directory structure, as discussed in the previous chapter. Let's open the command console, go the C:MVNconsumerBanking directory and execute the following mvn command. Maven will start building the project.

How to add JUnit as a test framework in Maven?

Here you can see, Maven already added Junit as test framework. By default, Maven adds a source file App.java and a test file AppTest.java in its default directory structure, as discussed in the previous chapter. Let's open the command console, go the C:\MVN\consumerBanking directory and execute the following mvn command.


2 Answers

Please check your target/test-classes directory for resource.xml. Your file should be here. If you find this file, I think the same name is the problem. Try rename the file for example to test-resource.xml. If the file doesn't exists in the test-classes directory, you should configure resource management in the maven pom.xml. Use maven-resouce-plugin. Hope it helps.

like image 127
lepike Avatar answered Sep 28 '22 00:09

lepike


Can't you give it another name ?
The Loader doesn't know that your resource.xml is in src/main/resources or src/test/resources, it's only a path to look for. I think the problem it's not Maven, this is how the Loader should and it works. From Maven would you expect to discard all classes/resources from src/main/resources when doing the test phase ?

like image 28
Cosmin Cosmin Avatar answered Sep 28 '22 00:09

Cosmin Cosmin