Liping Huang
Liping Huang

Reputation: 4476

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.

Upvotes: 6

Views: 21165

Answers (3)

ᄂ ᄀ
ᄂ ᄀ

Reputation: 5792

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.

Upvotes: 6

Zilvinas
Zilvinas

Reputation: 5578

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.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 692121

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.

Upvotes: 6

Related Questions