Legacy CGLib-based class imposteriser for jMock mocking framework
npx @tessl/cli install tessl/maven-org-jmock--jmock-legacy@2.13.0jMock Legacy provides CGLib-based class imposterization for the jMock 2 mocking framework. It enables mocking of concrete classes without calling their constructors, primarily for backward compatibility with older jMock applications. This component is deprecated in favor of ByteBuddy implementations due to weak Java 11 support.
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-legacy</artifactId>
<version>2.13.1</version>
</dependency>import org.jmock.lib.legacy.ClassImposteriser;
import org.jmock.api.Imposteriser;import org.jmock.lib.legacy.ClassImposteriser;
import org.jmock.api.Imposteriser;
import org.jmock.api.Invokable;
// Get the singleton instance
Imposteriser imposteriser = ClassImposteriser.INSTANCE;
// Check if a class can be mocked
if (imposteriser.canImposterise(MyConcreteClass.class)) {
// Create a mock instance
MyConcreteClass mock = imposteriser.imposterise(
mockInvokable,
MyConcreteClass.class
);
}
// Mock with additional interfaces
MyConcreteClass mock = imposteriser.imposterise(
mockInvokable,
MyConcreteClass.class,
AdditionalInterface.class,
AnotherInterface.class
);ClassImposteriser uses CGLib proxies and Objenesis for runtime class enhancement:
Determines whether a given class type can be imposterised by this implementation.
boolean canImposterise(Class<?> type);Parameters:
type (Class<?>): The class type to check for imposterisation capabilityReturns:
boolean: true if the type can be imposterised, false otherwiseConstraints:
Creates a mock object (imposter) that forwards method invocations to the provided Invokable handler.
<T> T imposterise(Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes);Parameters:
mockObject (Invokable): The handler that will receive forwarded method invocationsmockedType (Class<T>): The primary class or interface to mockancilliaryTypes (Class<?>...): Additional interfaces the mock should implement (varargs)Returns:
T: Mock instance that can be cast to mockedType and implements all ancilliaryTypesThrows:
IllegalArgumentException: If mockedType has a final toString method or cannot be imposterisedIllegalArgumentException: If CGLib proxy creation failsNotes:
Static singleton instance providing the primary access point for the imposteriser.
public static final Imposteriser INSTANCE;Description:
/**
* Interface for creating proxy objects that capture method invocations
* and forward them to Invokable handlers for mocking or stubbing
*/
public interface Imposteriser {
boolean canImposterise(Class<?> type);
<T> T imposterise(Invokable mockObject, Class<T> mockedType, Class<?>... ancilliaryTypes);
}/**
* Handler interface for receiving method invocations from mock objects
*/
public interface Invokable {
Object invoke(Invocation invocation) throws Throwable;
}
/**
* Interface for building textual descriptions of objects
*/
public interface Description {
Description appendText(String text);
Description appendValueList(String start, String separator, String end, Object... values);
}
/**
* Interface for objects that can describe themselves to a Description
*/
public interface SelfDescribing {
void describeTo(Description description);
}
/**
* Represents a method invocation on a mock object
*/
public class Invocation implements SelfDescribing {
public static final Object[] NO_PARAMETERS = null;
public enum ExpectationMode {
BUILDING, // We're building the expectations so invocations can return null
ASSERTING, // Return the default or specified reply
LEGACY // Legacy mode old imposters
}
public Invocation(ExpectationMode mode, Object invoked, Method method, Object... parameterValues);
public Invocation(ExpectationMode mode, Invocation other);
public Invocation(Object invoked, Method method, Object... parameterValues);
public Object getInvokedObject();
public Method getInvokedMethod();
public int getParameterCount();
public Object getParameter(int i);
public Object[] getParametersAsArray();
public Object applyTo(Object target) throws Throwable;
public void checkReturnTypeCompatibility(Object value);
public boolean isBuildingExpectation();
// SelfDescribing interface
public void describeTo(Description description);
}/**
* Utility class used internally to work around CGLib limitations
* when mocking Object.class
*/
public static class ClassWithSuperclassToWorkAroundCglibBug {
// Empty class used as CGLib proxy superclass
}IllegalArgumentException: Thrown when attempting to imposterise unsupported types
IllegalStateException: Thrown for reflection security issues
canImposterise() before calling imposterise()IllegalArgumentException for unsupported class typesThis library is marked as deprecated. For new development or Java 11+ compatibility, migrate to:
// Recommended alternative
import org.jmock.lib.imposters.ByteBuddyClassImposteriser;
Imposteriser imposteriser = ByteBuddyClassImposteriser.INSTANCE;