Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File not found when using Intellij IDEA

I have a file named test-mule-flow.xml under src/main/app folder, and I added a unit test:

MuleFlowTest extends FunctionalTestCase
@Override
public String getConfigResources() {
    return "src/main/app/test-mule-flow.xml";
}

@Test
public void test......

Actually those tests are executed correctly when I execute it in Eclipse(Mule Studio) and also in maven build, but unfortunately these tests are failed in Intellij IDEA version 12.

And I suppose this is caused by the classloader, in IDEA, the sources files are house in target/classes folder, and the test class are house in target/test-classes, this is maybe the reason, but how can I configure it? and it it better in maven way, I don't want to do some move configuration in idea manually.

Thanks.

like image 716
Liping Huang Avatar asked May 20 '13 05:05

Liping Huang


3 Answers

It looks like you're using a relative file path to load the file. So the actual result will depend on the directory from which the JVM executing your tests is started (it's "current" directory).

This is generally a bad idea. If the file is supposed to get bundled with the application, store it is src/main/resources and load it using the class loader:

InputStream in = MuleFlowTest.class.getResourceAsStream("/test-mule-flow.xml"));

If it's a file used only by your tests, store it in src/test/resources, and load it using the class loader (same as above).

If you really need to load it as a file, then ensure that the current directory is always the right one, or pass a system property telling the code what base directory it should use to create an absolute path.

like image 83
JB Nizet Avatar answered Sep 20 '22 00:09

JB Nizet


You have a relative path specified and you didn't give details on how the file is actually read from that location.

In case it is read from classpath, you should make sure the file is included. With Maven there is a convention to put resources into src\main\resources or src\test\resources. Maven-aware IDEs usually add these locations to classpath automatically. If for some reason you cannot keep your file in resources or other location included in classpath, you can manually configure source directories for project in IDEA.

In case the file is read from filesystem, you should check what you have set as a working directory in corresponding Run/Debug Configuration in IDEA.

like image 26
ᄂ ᄀ Avatar answered Sep 20 '22 00:09

ᄂ ᄀ


Maven uses the following 4 folders for its class path:

  • src/main/java - outputs in target/classes
  • src/main/resources - outputs in target/classes
  • src/test/java - outputs in target/test-classes
  • src/test/resources - outputs in target/test-classes

If you want to use any other folder for your tests ( and you have a decent reason for that ) then you should specify it explicitly for maven resource plugin as described in http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

Also, you should not use files from src folder in your tests, but rather from target ( and you shouldn't be specifying "target" either, just use the file name ), which is already in JVM's classpath.

like image 41
Zilvinas Avatar answered Sep 18 '22 00:09

Zilvinas