Package org.powermock.core.classloader
Class MockClassLoader
java.lang.Object
java.lang.ClassLoader
javassist.Loader
org.powermock.core.classloader.DeferSupportingClassLoader
org.powermock.core.classloader.MockClassLoader
Mock all classes except system classes.
Notice that there are two different types of classes that are not mocked:
- system classes are deferred to another classloader
- testing framework classes are loaded, but not modified
-
Nested Class Summary
Nested classes/interfaces inherited from class javassist.Loader
javassist.Loader.Simple
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private static final String
private final javassist.ClassPool
private final JavaAssistClassMarker
private List<MockTransformer>
static final String
Pass this string to the constructor to indicate that all classes should be modified.private static final String[]
private final String[]
private final String[]
Fields inherited from class org.powermock.core.classloader.DeferSupportingClassLoader
deferPackages, deferTo
Fields inherited from class javassist.Loader
doDelegation
-
Constructor Summary
ConstructorsConstructorDescriptionMockClassLoader
(String[] classesToMock) Creates a new instance of theMockClassLoader
based on the following parameters:MockClassLoader
(String[] classesToMock, String[] packagesToDefer) Creates a new instance of theMockClassLoader
based on the following parameters:MockClassLoader
(String[] classesToMock, String[] packagesToDefer, UseClassPathAdjuster useClassPathAdjuster) Creates a new instance of theMockClassLoader
based on the following parameters:MockClassLoader
(String[] classesToMock, UseClassPathAdjuster useClassPathAdjuster) Creates a new instance of theMockClassLoader
based on the following parameters: -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addClassesToModify
(String... classes) Add classes that will be loaded by the mock classloader, i.e.private static String[]
getPackagesToDefer
(String[] additionalDeferPackages) private Class<?>
loadMockClass
(String name) Load a mocked version of the class.protected Class<?>
private Class<?>
loadUnmockedClass
(String name, ProtectionDomain protectionDomain) void
setMockTransformerChain
(List<MockTransformer> mockTransformerChain) protected boolean
shouldLoadUnmodifiedClass
(String className) private boolean
private boolean
shouldModify
(String className) private boolean
protected boolean
Methods inherited from class org.powermock.core.classloader.DeferSupportingClassLoader
addIgnorePackage, cache, findResource, findResources, getResource, getResourceAsStream, getResources, loadClass, shouldDefer, shouldIgnore, shouldModify
Methods inherited from class javassist.Loader
addTranslator, delegateLoadingOf, delegateToParent, findClass, loadClassByDelegation, main, run, run, setClassPool, setDomain
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, 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:
-
CGLIB_ENHANCER
- See Also:
-
CGLIB_METHOD_WRAPPER
- See Also:
-
javaAssistClassMarker
-
mockTransformerChain
-
modify
-
packagesToLoadButNotModify
-
specificClassesToLoadButNotModify
-
packagesToBeDeferred
-
classPool
private final javassist.ClassPool classPool
-
-
Constructor Details
-
MockClassLoader
public MockClassLoader(String[] classesToMock, String[] packagesToDefer, UseClassPathAdjuster useClassPathAdjuster) Creates a new instance of theMockClassLoader
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
MockClassLoader() -
MockClassLoader
Creates a new instance of theMockClassLoader
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
Creates a new instance of theMockClassLoader
based on the following parameters:- Parameters:
classesToMock
- The classes that must be modified to prepare for testability.
-
MockClassLoader
Creates a new instance of theMockClassLoader
based on the following parameters:- Parameters:
classesToMock
- The classes that must be modified to prepare for testability.
-
-
Method Details
-
getPackagesToDefer
-
addClassesToModify
Add classes that will be loaded by the mock classloader, i.e. these classes will be byte-code manipulated to allow for testing. Any classes contained in thepackagesToBeDeferred
will be ignored. How ever classes added here have precedence over additionally deferred (ignored) packages (those ignored by the user using @PrepareForTest).- Parameters:
classes
- The fully qualified name of the classes that will be appended to the list of classes that will be byte-code modified to enable testability.
-
loadModifiedClass
- Specified by:
loadModifiedClass
in classDeferSupportingClassLoader
- Throws:
ClassFormatError
ClassNotFoundException
-
shouldModify
-
shouldModifyAll
private boolean shouldModifyAll() -
loadUnmockedClass
private Class<?> loadUnmockedClass(String name, ProtectionDomain protectionDomain) throws ClassFormatError, ClassNotFoundException -
loadMockClass
Load a mocked version of the class. -
setMockTransformerChain
-
shouldModifyClass
- Specified by:
shouldModifyClass
in classDeferSupportingClassLoader
-
shouldLoadUnmodifiedClass
- Specified by:
shouldLoadUnmodifiedClass
in classDeferSupportingClassLoader
-
shouldLoadWithMockClassloaderWithoutModifications
-