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 use
Java
I wrote all the documents.java
File, and to run it, you need to compile it into.class
Bytecode files can beJVM
Run; This would require theJVM
So let’s find the corresponding.class
That’s what it’s all aboutclasspath
. JVM
When compiling the project, will actively.java
File compiled into.class
File and andresources
Static files in the directorytarget/classes
(if it istest
The following class will be placed in/target/test-classes
Under) directory; The existing project catalog is as follows:
Compile entry
target
View 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 print
With 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 fromclasspath
Gets the root path of
2,getClassLoader().getResource("/")
Printed with “/” asnull
The pathCannot have a "/"
- Now keep trying to get
resources
The files under the2.txt
and3.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 classclass.getResource()
With “/” fromclasspath
Gets the root path ofclass.getResource()
It’s essentially calledgetClassLoader
It’s just encapsulated so we can use itgetClassLoader().getResource("")
No “/” when fromclasspath
Gets the root path ofgetClassLoader().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