I am trying to implement some unit testing for an old framework. I am attempting to mock out the database layer. Unfortunately our framework is a bit old and not quite using best practices so there is no clear separation of concerns. I am bit worried that trying to mock out the database layer might make the JVM load a huge number of classes that won't even be used.
I don't really understand class loaders that well so this might not be a problem. Is there a way to take a peak at all the classes a particular ClassLoader has loaded to prove what is going on under the hood?
To know the ClassLoader that loads a class the getClassLoader() method is used. All classes are loaded based on their names and if any of these classes are not found then it returns a NoClassDefFoundError or ClassNotFoundException.
The most straightforward approach for listing all classes loaded would be to log that in a console output or file. [Opened /Library/Java/JavaVirtualMachines/jdk1. 8.0_241. jdk/Contents/Home/jre/lib/rt.
As we can see, there are three different class loaders here: application, extension, and bootstrap (displayed as null). The application class loader loads the class where the example method is contained. An application or system class loader loads our own files in the classpath.
You can get all classpath roots by passing an empty String into ClassLoader#getResources() . Enumeration<URL> roots = classLoader. getResources("");
You can create your own Classloader and use that to load during the unit test. Have your own custom Classloader print out what it's doing.
Or if you just want to know which classes are loaded, do:
java -verbose:class
Be warned that using
java -verbose
Will produce an enormous amount of output. Log the output to a file and then use grep. If you have the 'tee' filter you could try this:
java -verbose | tee classloader.log
grep class classloader.log
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With