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 Stringprivate static final Stringprivate final javassist.ClassPoolprivate final JavaAssistClassMarkerprivate List<MockTransformer> static final StringPass 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, deferToFields inherited from class javassist.Loader
doDelegation -
Constructor Summary
ConstructorsConstructorDescriptionMockClassLoader(String[] classesToMock) Creates a new instance of theMockClassLoaderbased on the following parameters:MockClassLoader(String[] classesToMock, String[] packagesToDefer) Creates a new instance of theMockClassLoaderbased on the following parameters:MockClassLoader(String[] classesToMock, String[] packagesToDefer, UseClassPathAdjuster useClassPathAdjuster) Creates a new instance of theMockClassLoaderbased on the following parameters:MockClassLoader(String[] classesToMock, UseClassPathAdjuster useClassPathAdjuster) Creates a new instance of theMockClassLoaderbased on the following parameters: -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddClassesToModify(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) voidsetMockTransformerChain(List<MockTransformer> mockTransformerChain) protected booleanshouldLoadUnmodifiedClass(String className) private booleanprivate booleanshouldModify(String className) private booleanprotected booleanMethods inherited from class org.powermock.core.classloader.DeferSupportingClassLoader
addIgnorePackage, cache, findResource, findResources, getResource, getResourceAsStream, getResources, loadClass, shouldDefer, shouldIgnore, shouldModifyMethods inherited from class javassist.Loader
addTranslator, delegateLoadingOf, delegateToParent, findClass, loadClassByDelegation, main, run, run, setClassPool, setDomainMethods 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 theMockClassLoaderbased 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 theMockClassLoaderbased 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 theMockClassLoaderbased on the following parameters:- Parameters:
classesToMock- The classes that must be modified to prepare for testability.
-
MockClassLoader
Creates a new instance of theMockClassLoaderbased 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 thepackagesToBeDeferredwill 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:
loadModifiedClassin classDeferSupportingClassLoader- Throws:
ClassFormatErrorClassNotFoundException
-
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:
shouldModifyClassin classDeferSupportingClassLoader
-
shouldLoadUnmodifiedClass
- Specified by:
shouldLoadUnmodifiedClassin classDeferSupportingClassLoader
-
shouldLoadWithMockClassloaderWithoutModifications
-