During unit testing, a debug error suddenly appeared
1. Suspected breakpoint is invalid
By setting a breakpoint, but it can’t run at the breakpoint, so the blogger thinks that the breakpoint is invalid, and there are various reasons for finding the breakpoint error on the Internet (I believe it will be encountered in the future)
By adding in the first line of the test class method body:
int intNum = 1;
Tested the breakpoint, and suddenly found that it was not the breakpoint that failed, but that the first line of code was wrong, so the first problem was solved smoothly
2. Class static method error
Static variables of a class, static methods are loaded into memory together with the class, therefore, errors in static methods must be thrown prior to errors in object methods, object methods belong to objects, and will only be thrown when they are called by objects
Locate the second class static method problem
3. Java web project read resource file path problem
Through code splitting: split the simplified code one by one with multiple reference variables, such as
properties.load(Druid.class.getClassLoader().getResourceAsStream("druid.properties"));
split into
ClassLoader classLoader = Druid.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("druid.properties");
properties.load(inputStream);
Debugging through Step Into (F7) (click the method body to be into) and found that the value obtained by
my inputStream is null
It shows that the class has not successfully read the resource file "druid.properties", and my druid.properties path is src/druid.properties.
I saw the explanation of the Great God from the Internet:
The way the first class loader loads resource files:
class.getClassLoader().getResourceAsStream(String name)
By default, files are found from ==classpath (that is, "project root directory/target/classes")==. The
name must be relative to the classpath, that is, the relative path of the current class package file, and cannot contain "/", otherwise a null pointer will be thrown .
The JVM copies the files in the "project root directory /src/main/resources" to the classpath directory during automatic compilation, so put druid.properties in the project root directory /src/main/resources , after compilation , the absolute path corresponding to the relative path "druid.properties" in the source code is **"project root directory/target/classes/druid.properties"**
InputStream inputStream = Druid.class.getClassLoader().getResourceAsStream("druid.properties");
The resource file was successfully loaded, and this error was fixed!
Replenish
In addition, add a second method of class loader loading resource files:
class.getResourceAsStream(String name) ,
name must use an absolute path with the classpath as the root directory, and the absolute path uses "/" as the path header, so
InputStream inputStream = Druid.class.getResourceAsStream("/druid.properties");
reference
Java project reads resources resource file path
[IntelliJ IDEA] Debug debugging use record