Results 1 to 7 of 7
  1. #1
    bobBarclay is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Problem: Maven test in different working directory from test resources

    Hi, I have a problem with Maven (I am still new to Maven) and I was hoping for some help. I have a fairly odd problem. I am running Ubuntu 16.04, and I have a maven project in /home/ikba/Sum2016/ci, with tests within /home/ikba/Sum2016/ci/src/test/java, and test resources within /home/ikba/Sum2016/ci/src/test/resources. One of the files I have is /home/ikba/Sum2016/ci/src/test/resources/cpu.arff, but when I try to access ./cpu.arff in the JUnit tests within the test folder, I get a FileNotFound problem.

    Investigating further, I used the debut option to get the following information for the tests (this seems to agree with some code I added to a test to print the working directory):

    [DEBUG] (s) testClassesDirectory = /home/ikba/Sum2016/ci/target/test-classes
    [DEBUG] (s) testSourceDirectory = /home/ikba/Sum2016/ci/src/test/java
    [DEBUG] (s) workingDirectory = /home/ikba/Sum2016/ci

    Additionally, Maven seems to be working properly, and is copying the test resources to /home/ikba/Sum2016/ci/target/test-classes. Sure enough, the test can find ./target/test-classes/cpu.arff without problem. However, it is my understanding that this is not the expected behaviour. My POM file is below:

    Java Code:
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>edu.toronto.cs.se.ci</groupId>
    <artifactId>ci</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ci</name>
    <url>http://maven.apache.org</url>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>17.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math</artifactId>
    <version>2.2</version></dependency>
    <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20141113</version>
    </dependency>
    <dependency>
    <groupId>nz.ac.waikato.cms.weka</groupId>
    <artifactId>weka-stable</artifactId>
    <version>3.8.0</version>
    </dependency>
    <dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.8.2</version></dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
    Any help would be appreciated. Thanks!
    Last edited by bobBarclay; 07-22-2016 at 02:19 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Problem: Maven test in different working directory from test resources

    Can you please post the POM inside code tags.
    [code] in here [/code]

    And ensure it is formatted, otherwise it's almost impossible to read.

    Not that I expect it to hold the answer.
    How are you referencing that resource?
    That's probably more important.
    And what error exactly are you getting?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    bobBarclay is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Re: Problem: Maven test in different working directory from test resources

    I am accessing the file as follows:
    Java Code:
    public void testFilePresence() throws FileNotFoundException{
        	File file = new File("./cpu.arff");
        	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    }
    I have placed these lines by themselves in a test method in one of the JUnit tests.
    When run in eclipse I get the following trace:
    java.io.FileNotFoundException: ./cpu.arff (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java: 195)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at edu.toronto.cs.se.ci.AppTest.testFilePresence(AppT est.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:131)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:192)

    When I run the command line "mvn test" I get a shortened version of the same trace:
    java.io.FileNotFoundException: ./cpu.arff (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java: 195)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at edu.toronto.cs.se.ci.AppTest.testFilePresence(AppT est.java:67)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Problem: Maven test in different working directory from test resources

    Log the value of file.getAbsolutePath to see exactly where on your file system it is looking.

    I would have expected you to get this as a resource, though.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    bobBarclay is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Re: Problem: Maven test in different working directory from test resources

    The path in which the file system is looking is: /home/ikba/Sum2016/ci/./cpu.arff

    To contrast, if I change the test's code to
    Java Code:
     public void testFilePresence() throws FileNotFoundException{
        	File file = new File("./target/test-classes/cpu.arff");
        	System.out.println("Abs path: "+file.getAbsolutePath());
        	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        }
    Then the test runs without any errors, and the absolute path becomes /home/ikba/Sum2016/ci/./target/test-classes/cpu.arff

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Problem: Maven test in different working directory from test resources

    That's the problem with relative paths - they're the exact opposite of specific and when things are not specific you end up with undefined behavior. In this case it very much depends on what the current working directory is what the absolute path is going to end up being.

    There is a cross-platform, zero problem way of loading files in Java applications: loading them as classpath resources. Then it works on Windows, Linux, Mac, it works in regular code and your tests and it will work on any computer you deploy the application to without any risk of the file not existing in the right spot.

    Ex:

    https://www.mkyong.com/java/java-rea...ources-folder/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    bobBarclay is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Re: Problem: Maven test in different working directory from test resources

    Thanks!

Similar Threads

  1. maven-spring-boot integration test
    By flyingBird in forum Apache Maven
    Replies: 0
    Last Post: 04-06-2016, 01:11 PM
  2. Maven always run test
    By dmitry in forum Apache Maven
    Replies: 2
    Last Post: 02-23-2016, 12:15 AM
  3. Maven ignoring new test files
    By Nissem in forum Apache Maven
    Replies: 2
    Last Post: 09-05-2015, 08:14 AM
  4. Replies: 0
    Last Post: 04-11-2014, 01:15 PM
  5. Error with a JUnit test using maven surefire plugin
    By masijade in forum Apache Maven
    Replies: 0
    Last Post: 03-16-2012, 02:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •