Results 1 to 8 of 8
  1. #1
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Junit test program can't read files in src/test/resource

    Junit test program can't read files in src/test/resource-11.png

    Java Code:
    
        public class AppTest 
        {
    	@Test
    	public void test3(){
    		try {
    			InputStream is = new BufferedInputStream(new FileInputStream(new File("example.xml")));
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
        }
    I get exception:

    Java Code:
        java.io.FileNotFoundException: /example.xml (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.java:138)
    	at nanger.Misc.AppTest.test3(AppTest.java:47)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        ...
    why Junit test program can't read files in src/test/resource?
    Attached Thumbnails Attached Thumbnails Junit test program can't read files in src/test/resource-111.png  
    Last edited by esolve; 02-05-2017 at 05:56 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,990
    Rep Power
    32

    Default Re: Junit test program can't read files in src/test/resource

    To see where the program is looking for the file, create a File object and print its absolute path, add this statement to your code:
    System.out.println(new File("example.xml").getAbsolutePath());
    Last edited by Norm; 02-05-2017 at 06:48 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Junit test program can't read files in src/test/resource

    Because you using the default directory which is probably not where the file is located. Read the API. Use some of the available classes to display the path name you are using. If you don't know which classes to use, look at the API and try and figure it out. Check with the io package.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Re: Junit test program can't read files in src/test/resource

    Quote Originally Posted by Norm View Post
    To see where the program is looking for the file, create a File object and print its absolute path, add this statement to your code:
    System.out.println(new File("example.xml").getAbsolutePath());

    the result is

    workspace/group1/Misc/example.xml

    what can I do?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,990
    Rep Power
    32

    Default Re: Junit test program can't read files in src/test/resource

    what can I do?
    Move the file to the location where the program is looking.
    or Change the path used in the program to point to where the file is located.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    esolve is offline Member
    Join Date
    Mar 2015
    Posts
    56
    Rep Power
    0

    Default Re: Junit test program can't read files in src/test/resource

    Quote Originally Posted by Norm View Post
    Move the file to the location where the program is looking.
    or Change the path used in the program to point to where the file is located.
    I don't get it, according to my understanding, src/test/java and src/test/resources should be in the same directory, so a method in a java class which is in src/test/java should be able to access a resource file in src/test/resources directly without indicating the path, why in my project, it is not the case?

    also, I don't get what you mean, what do you mean by "the location the program is looking", and how to change the path used in the program?

    and it is strange that this statement (System.out.println(new File("example.xml").getAbsolutePath()); ) can access "example.xml" directly and successfully, why can't the statement "InputStream is = new BufferedInputStream(new FileInputStream(new File("example.xml")));" access it successfully?

    BTW:
    the real path of "example.xml" is workspace/group1/Misc/src/test/resources/example.xml
    but "getAbsolutePath()" returns workspace/group1/Misc/example.xml
    Last edited by esolve; 02-05-2017 at 07:28 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,990
    Rep Power
    32

    Default Re: Junit test program can't read files in src/test/resource

    strange that this statement (System.out.println(new File("example.xml").getAbsolutePath()); ) can access "example.xml" directly and successfully
    The File object that was printed did not access any example.xml file. It created a path to where if would look for the file if it was used either for input or for output.

    Try copying the example.xml file to the Misc folder and see if the program finds it.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Junit test program can't read files in src/test/resource

    Path are resolved relative to the current directory
    Find the current directory in Java

    Since you're hard-coding and it seems to be read only, you might as well use the classloader, which will find it irrespective of the current directory
    Java Code:
    InputStream is = new BufferedInputStream(getClass().getResourceAsStream("/test/resources/example.xml"));

Similar Threads

  1. Problems with JUnit test in a program
    By pokadean in forum New To Java
    Replies: 5
    Last Post: 07-25-2013, 08:50 PM
  2. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  3. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  4. The requested resource (/Test.war/index.jsp) is not available.
    By rameshraj in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 04-05-2009, 11:41 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM

Posting Permissions

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