Package com.helger.commons.mock
Class CommonsMock
java.lang.Object
com.helger.commons.mock.CommonsMock
Mock objects by invoking their constructors with arbitrary objects. It
separates into static mocking rules and "per instance" mocking rules. Static
mocking rules apply to all instances of this class whereas "per instance"
mocking rules apply only to this instance.
- Author:
- Philip Helger
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThis class represents a parameter description for a single mockable type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TCreate a mock instance of the passed class.<T> ICommonsList<T> Create aListof mocked objects.<T> ICommonsSet<T> Create aICommonsSetof mocked objects.voidregisterPerInstance(com.helger.commons.mock.CommonsMock.MockSupplier aSupplier) Register an arbitrary MockSupplier.<T> voidregisterPerInstance(Class<T> aDstClass, CommonsMock.Param[] aParams, Function<IGetterDirectTrait[], T> aSupplier) Create a mock supplier with parameters.<T> voidregisterPerInstance(Class<T> aClass, Supplier<T> aSupplier) Register a simple supplier (=factory) to be invoked when an instance of the passed class is to be mocked.<T> voidregisterPerInstanceConstant(T aObject) Register a constant mock object.static voidregisterStatic(com.helger.commons.mock.CommonsMock.MockSupplier aSupplier) Register an arbitrary MockSupplier that is available across tests!static <T> voidregisterStatic(Class<T> aDstClass, CommonsMock.Param[] aParams, Function<IGetterDirectTrait[], T> aSupplier) Create a mock supplier with parameters.static <T> voidregisterStatic(Class<T> aClass, Supplier<T> aSupplier) Register a simple supplier (=factory) to be invoked when an instance of the passed class is to be mocked.static <T> voidregisterStaticConstant(T aObject) Register a constant mock object.
-
Constructor Details
-
CommonsMock
public CommonsMock()
-
-
Method Details
-
registerStaticConstant
Register a constant mock object. That class will always be mocked with the specified instance.- Type Parameters:
T- The type to be mocked- Parameters:
aObject- The object to be used as a mock result. May not benull.
-
registerStatic
Register a simple supplier (=factory) to be invoked when an instance of the passed class is to be mocked. This method does not give you any possibility to provide parameters and so this works only if mock instance creation is fixed.- Type Parameters:
T- The type to be mocked- Parameters:
aClass- The class to be mocked. May not benull.aSupplier- The supplier/factory to be invoked when to mock this class. May not benull.
-
registerStatic
public static <T> void registerStatic(@Nonnull Class<T> aDstClass, @Nonnull CommonsMock.Param[] aParams, @Nonnull Function<IGetterDirectTrait[], T> aSupplier) Create a mock supplier with parameters.- Type Parameters:
T- The type to be mocked- Parameters:
aDstClass- The destination class to be mocked. May not benull.aParams- The parameter declarations to be used. May not benull.aSupplier- The generic function to be invoked. Must take an array ofIGetterDirectTraitand return an instance of the passed class.
-
registerStatic
public static void registerStatic(@Nonnull com.helger.commons.mock.CommonsMock.MockSupplier aSupplier) Register an arbitrary MockSupplier that is available across tests!- Parameters:
aSupplier- The supplier to be registered. May not benull.
-
registerPerInstanceConstant
Register a constant mock object. That class will always be mocked with the specified instance.- Type Parameters:
T- The type to be mocked- Parameters:
aObject- The object to be used as a mock result. May not benull.
-
registerPerInstance
Register a simple supplier (=factory) to be invoked when an instance of the passed class is to be mocked. This method does not give you any possibility to provide parameters and so this works only if mock instance creation is fixed.- Type Parameters:
T- The type to be mocked- Parameters:
aClass- The class to be mocked. May not benull.aSupplier- The supplier/factory to be invoked when to mock this class. May not benull.
-
registerPerInstance
public <T> void registerPerInstance(@Nonnull Class<T> aDstClass, @Nonnull CommonsMock.Param[] aParams, @Nonnull Function<IGetterDirectTrait[], T> aSupplier) Create a mock supplier with parameters.- Type Parameters:
T- The type to be mocked- Parameters:
aDstClass- The destination class to be mocked. May not benull.aParams- The parameter declarations to be used. May not benull.aSupplier- The generic function to be invoked. Must take an array ofIGetterDirectTraitand return an instance of the passed class.
-
registerPerInstance
public void registerPerInstance(@Nonnull com.helger.commons.mock.CommonsMock.MockSupplier aSupplier) Register an arbitrary MockSupplier.- Parameters:
aSupplier- The supplier to be registered. May not benull.
-
mock
Create a mock instance of the passed class.- Type Parameters:
T- The type to be mocked- Parameters:
aClass- The class to be mocked. May not benull.aParams- An optional array of parameters to be passed to the mocking supplier. May benullor empty.- Returns:
- The mocked object. Never
null. - Throws:
IllegalStateException- If an exception occurred during the mock instance creation.
-
mockMany
@Nonnull @ReturnsMutableCopy public <T> ICommonsList<T> mockMany(@Nonnegative int nCount, @Nonnull Class<T> aClass, @Nullable Object... aParams) Create aListof mocked objects.- Type Parameters:
T- The type to be mocked- Parameters:
nCount- Number of objects to be mocked. Must be ≥ 0.aClass- The class to be mocked.aParams- An optional array of parameters to be passed to the mocking supplier for each object to be mocked. May benullor empty.- Returns:
- The list with
nCountentries.
-
mockSet
@Nonnull @ReturnsMutableCopy public <T> ICommonsSet<T> mockSet(@Nonnegative int nCount, @Nonnull Class<T> aClass, @Nullable Object... aParams) Create aICommonsSetof mocked objects.- Type Parameters:
T- The type to be mocked- Parameters:
nCount- Number of objects to be mocked. Must be ≥ 0.aClass- The class to be mocked.aParams- An optional array of parameters to be passed to the mocking supplier for each object to be mocked. May benullor empty.- Returns:
- The set with
nCountentries.
-