Class MockClassLoader
java.lang.Object
java.lang.ClassLoader
org.powermock.core.classloader.MockClassLoader
- Direct Known Subclasses:
JavassistMockClassLoader
The classloader loads and modified all classes except:
- system classes. They are deferred to system classloader
- classes that locate in packages that specified as packages to ignore with using
MockClassLoaderConfiguration.addIgnorePackage(String...)
Testing frameworks classes are loaded, but not modified.
The classloader uses list of MockTransformer to modify classes during loading.
- Author:
- Johan Haleby, Jan Kronquist, Artur Zagretdinov
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ClassMarkerprotected ClassWrapperFactorystatic final StringPass this string to the constructor to indicate that all classes should be modified. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMockClassLoader(String[] classesToMock, String[] packagesToDefer) Creates a new instance of the based on the following parameters:protectedMockClassLoader(MockClassLoaderConfiguration configuration, ClassWrapperFactory classWrapperFactory) Creates a new instance of the based on the following parameters: -
Method Summary
Modifier and TypeMethodDescriptionvoidRegister a class to the cache of this classloaderprotected abstract byte[]defineAndTransformClass(String name, ProtectionDomain protectionDomain) Class<?> defineClass(String name, ProtectionDomain protectionDomain, byte[] clazz) protected URLfindResource(String name) Finds the resource with the specified name on the search path.protected Enumeration<URL> findResources(String name) protected ObjectgetClassLoadingLock(String className) getResources(String name) protected Class<?> protected Class<?> loadClassByThisClassLoader(String className) protected Class<?> loadUnmockedClass(String name, ProtectionDomain protectionDomain) voidsetMockTransformerChain(MockTransformerChain mockTransformerChain) protected <T> ClassWrapper<T> transformClass(ClassWrapper<T> wrappedType) Methods inherited from class ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
MODIFY_ALL_CLASSES
Pass this string to the constructor to indicate that all classes should be modified.- See Also:
-
classMarker
-
classWrapperFactory
-
-
Constructor Details
-
MockClassLoader
Creates a new instance of the based on the following parameters:- Parameters:
classesToMock- The classes that must be modified to prepare for testability.packagesToDefer- Classes in these packages will be defered to the system class-loader.
-
MockClassLoader
protected MockClassLoader(MockClassLoaderConfiguration configuration, ClassWrapperFactory classWrapperFactory) Creates a new instance of the based on the following parameters:- Parameters:
configuration- The configuration of class loader. Configuration contains information about classes which should be loaded by class loader, defer to system and mocked.classWrapperFactory- an instance ofClassWrapperFactorywhich is used to wrap internal framework's representation of the class intoClassWrapper- See Also:
-
-
Method Details
-
loadClassByThisClassLoader
protected Class<?> loadClassByThisClassLoader(String className) throws ClassFormatError, ClassNotFoundException -
setMockTransformerChain
-
getMockTransformerChain
-
loadUnmockedClass
protected Class<?> loadUnmockedClass(String name, ProtectionDomain protectionDomain) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
defineClass
-
transformClass
- Throws:
Exception
-
defineAndTransformClass
protected abstract byte[] defineAndTransformClass(String name, ProtectionDomain protectionDomain) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
getResource
- Overrides:
getResourcein classClassLoader
-
getResourceAsStream
- Overrides:
getResourceAsStreamin classClassLoader
-
getResources
- Overrides:
getResourcesin classClassLoader- Throws:
IOException
-
getConfiguration
-
cache
Register a class to the cache of this classloader -
loadClass
- Overrides:
loadClassin classClassLoader- Throws:
ClassNotFoundException
-
getClassLoadingLock
- Overrides:
getClassLoadingLockin classClassLoader
-
findResource
Finds the resource with the specified name on the search path.- Overrides:
findResourcein classClassLoader- Parameters:
name- the name of the resource- Returns:
- a
URLfor the resource, ornullif the resource could not be found.
-
findResources
- Overrides:
findResourcesin classClassLoader- Throws:
IOException
-