In daily automated test development, configuration files are often used, or environment configuration, or data driven, etc. These files are often placed in the resources directory and read by methods such as getResource, classLoader.getResource, and getResourceAsStream(). Here do comb share

What is classpath

The key to read resource files is to find the location of the file, ultimately is to find the path, and how to find, where to find is a problem; This has a lot to do with classpath, so take a look at Classpath first

  • We useJavaI wrote all the documents.javaFile, and to run it, you need to compile it into.classBytecode files can beJVMRun; This would require theJVMSo let’s find the corresponding.classThat’s what it’s all aboutclasspath.
  • JVMWhen compiling the project, will actively.javaFile compiled into.classFile and andresourcesStatic files in the directorytarget/classes(if it istestThe following class will be placed in/target/test-classesUnder) directory; The existing project catalog is as follows:

    Compile entrytargetView the following information in the directory:

2, class. GetResource ()

Let’s look at the use of getResource

  • First run the following test code, printing the path with and without “/”

    import org.junit.jupiter.api.Test;
    
    public class ResourceTestDemo {
    
        @Test
        void getResourceTest(a){
            System.out.println(ResourceTestDemo.class.getResource(""));
            System.out.println(ResourceTestDemo.class.getResource("/"));
           }
    Copy the code

    Print result:

    file:/Users/username/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/test-classes/resourcetest/
    file:/Users/username/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/test-classes/
    Copy the code

    GetResource (“”) without “/” obtains resources from the package path of the current class. GetResource (“/”) with “/” obtains resources from the root path of the classpath

  • Now try to get files 2.txt and 3.txt under Resources:

    Test code:

    @Test
     void getResourceFileTest(a){
         System.out.println(ResourceTestDemo.class.getResource("/3.txt"));
         System.out.println(ResourceTestDemo.class.getResource("/test/2.txt"));
     }
    Copy the code

    Print result:

    file:/Users/username/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/classes/3.txt
    file:/Users/username/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/classes/test/2.txt
    Copy the code

3, getClassLoader (). GetResource ()

  • As above, run the test code separately and printWith a "/"andWithout "/"thepath
     @Test
     void getClassLoaderResourceTest(a){
         System.out.println(ResourceTestDemo.class.getClassLoader().getResource(""));
         System.out.println(ResourceTestDemo.class.getClassLoader().getResource("/"));
     }
    Copy the code

    Print result:

    file:/Users/qinzhen/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/test-classes/
    null
    Copy the code

    Results analysis:

    1. getClassLoader().getResource("")No “/” when fromclasspathGets the root path of

    2, getClassLoader().getResource("/")Printed with “/” asnullThe pathCannot have a "/"

  • Now keep trying to getresourcesThe files under the2.txtand3.txt:
    @Test
    void getClassLoaderResourceFileTest(a){
        System.out.println(ResourceTestDemo.class.getClassLoader().getResource("3.txt"));
        System.out.println(ResourceTestDemo.class.getClassLoader().getResource("test/2.txt"));
    }
    Copy the code
    Print result:Copy the code
    file:/Users/qinzhen/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/classes/3.txt
    file:/Users/qinzhen/Documents/TestDev/MyTraining/XUnit/ResourceTest/target/classes/test/2.txt
    Copy the code

4, getResourceAsStream ()

The getResourceAsStream() method simply gets the input stream of the corresponding path file, the same usage of the path as getResource()

5, add

Class. GetResource source: class.getResource

public java.net.URL getResource(String name) {
    name = resolveName(name);
    ClassLoader cl = getClassLoader0();
    if (cl==null) {
        // A system class.
        return ClassLoader.getSystemResource(name);
    }
    return cl.getResource(name);
}
Copy the code

GetClassLoader is also called here, just for our convenience

6, summary

  • class.getResource()Without a “/”, the resource is retrieved from the package path of the current class
  • class.getResource()With “/” fromclasspathGets the root path of
  • class.getResource()It’s essentially calledgetClassLoaderIt’s just encapsulated so we can use it
  • getClassLoader().getResource("")No “/” when fromclasspathGets the root path of
  • getClassLoader().getResource("/")In the pathCannot have a "/"
  • getResourceAsStream()Method is simply to get the input stream of the corresponding path file, and the usage of the pathgetResource()consistent