Class AssistedFactoryMocker<T,U>
- Type Parameters:
T- The type of the class that is instantiated by the factory.U- The type of the factory that instantiates the target class.
AssistedFactory.
Any dependencies that cannot be mocked will default to null. These can later be manually updated using
injectParameter(Class, String, Object) if needed.
Example usage:
// The class with the AssistedInject constructor that we want to create a factory for.
public class MyObject {
@AssistedInject
MyObject(MyDependency myDependency,
@Assisted String someString) {
...
}
}
// The interface that represents the factory.
@AssistedFactory
public interface MyObjectFactory {
MyObject create(String someString);
}
// Create a new factory using an existing instance of MyDependency.
public MyObjectFactory createMyObjectFactory(MyDependency existingMyDependency) {
final var mocker = new AssistedFactoryMocker<>(MyObject.class, MyObjectFactory.class);
mocker.setMock(MyDependency.class, existingMyDependency);
return mocker.getFactory();
}
public MyObject createMyObject(MyObjectFactory factory, String someString) {
return factory.create(someString);
}
-
Constructor Summary
ConstructorsConstructorDescriptionAssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass) AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass, org.mockito.MockSettings mockSettings) AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass, org.mockito.stubbing.Answer<?> defaultAnswer) AssistedFactoryMocker(Class<U> factoryClass) Shortcut forAssistedFactoryMocker(Class, Class, MockSettings). -
Method Summary
Modifier and TypeMethodDescriptionGets the factory class that can be used to construct new instances of the target class using the values provided through the factory and the mocked instances for the remaining values.<V> VgetParameter(Class<V> type) <V> VgetParameter(Class<V> type, @Nullable String name) Gets the mocked object of the given type.static <T,U> AssistedFactoryMocker <T, U> injectMocksFromTestClass(Class<T> targetClass, Class<U> factoryClass, Object testInstance) Creates a newAssistedFactoryMockerinstance using the mocked objects from the provided test class.static <T,U> AssistedFactoryMocker <T, U> injectMocksFromTestClass(Class<U> factoryClass, Object testInstance) Creates a newAssistedFactoryMockerinstance using the mocked objects from the provided test class.<V> AssistedFactoryMocker<T, U> injectParameter(Class<V> type, @Nullable String name, V value) Sets the mocked instance of a constructor parameter.<V> AssistedFactoryMocker<T, U> injectParameter(Class<V> type, V parameter) injectParameter(Object param) Injects a single parameter into the factory method.injectParameters(Object... params) Injects the parameters in the factory method.
-
Constructor Details
-
AssistedFactoryMocker
public AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass, org.mockito.MockSettings mockSettings) throws IllegalArgumentException - Parameters:
targetClass- The class that is instantiated by the factory.factoryClass- The factory class that instantiates the target class.mockSettings- The mock settings that are used to create the injected objects for the factory (i.e. the objects that are not provided by the factory method).- Throws:
IllegalArgumentException- If the factory method or the target constructor could not be found.
-
AssistedFactoryMocker
public AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass, org.mockito.stubbing.Answer<?> defaultAnswer) throws IllegalArgumentException - Parameters:
targetClass- The class that is instantiated by the factory.factoryClass- The factory class that instantiates the target class.defaultAnswer- The default answer that is used for all mocks that are created for the factory.- Throws:
IllegalArgumentException- If the factory method or the target constructor could not be found.
-
AssistedFactoryMocker
public AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass) throws IllegalArgumentException - Parameters:
targetClass- The class that is instantiated by the factory.factoryClass- The factory class that instantiates the target class.- Throws:
IllegalArgumentException- If the factory method or the target constructor could not be found.
-
AssistedFactoryMocker
Shortcut forAssistedFactoryMocker(Class, Class, MockSettings).- Parameters:
factoryClass- The factory class that instantiates the target class.- Throws:
IllegalArgumentException- If the factory method or the target constructor could not be found.
-
-
Method Details
-
injectMocksFromTestClass
public static <T,U> AssistedFactoryMocker<T,U> injectMocksFromTestClass(Class<U> factoryClass, Object testInstance) throws IllegalArgumentException Creates a newAssistedFactoryMockerinstance using the mocked objects from the provided test class.This method is a shortcut for
injectMocksFromTestClass(Class, Class, Object).Specifically, it will look for fields in the test class that are annotated with
Mockand inject them into the factory method if any of the parameters in the factory method match the type of the field.- Type Parameters:
T- The type of the class that is instantiated by the factory.U- The type of the factory that instantiates the target class.- Parameters:
factoryClass- The factory class that instantiates the target class.testInstance- The instance of a test class that contains the mocked objects.- Returns:
- A new
AssistedFactoryMockerinstance. - Throws:
IllegalArgumentException- If the factory method or the target constructor could not be found.
-
injectMocksFromTestClass
public static <T,U> AssistedFactoryMocker<T,U> injectMocksFromTestClass(Class<T> targetClass, Class<U> factoryClass, Object testInstance) throws IllegalArgumentException Creates a newAssistedFactoryMockerinstance using the mocked objects from the provided test class.Specifically, it will look for fields in the test class that are annotated with
Mockand inject them into the factory method if any of the parameters in the factory method match the type of the field.- Type Parameters:
T- The type of the class that is instantiated by the factory.U- The type of the factory that instantiates the target class.- Parameters:
targetClass- The class that is instantiated by the factory.factoryClass- The factory class that instantiates the target class.testInstance- The instance of a test class that contains the mocked objects.- Returns:
- A new
AssistedFactoryMockerinstance. - Throws:
IllegalArgumentException- If the factory method or the target constructor could not be found.
-
getFactory
Gets the factory class that can be used to construct new instances of the target class using the values provided through the factory and the mocked instances for the remaining values.- Returns:
- The factory instance.
-
getParameter
SeegetParameter(Class, String).Shortcut for unnamed parameters.
-
getParameter
Gets the mocked object of the given type.- Type Parameters:
V- The type of the mocked object to get.- Parameters:
type- The type of the mocked object to get.name- The name of the mocked object, as specified byAssisted.value()in the constructor.- Returns:
- The mocked object of the given type with the given name.
- Throws:
IllegalArgumentException- when null values are not allowed and no existing mock exists for the given type and name combination.
-
injectParameters
Injects the parameters in the factory method.Note that all primitive types are automatically boxed and will not match any primitive types in the constructor.
Because no type is provided for the parameters, the type of the parameter is inferred from the value and will be compared against the type of the parameter in the constructor using
Class.isAssignableFrom(Class)instead of requiring an exact match.If it is required for the type to be exact, use
injectParameter(Class, Object)instead.Additionally, because this method does not know the name of the parameters, it will not match any named parameters in the constructor. To match named parameters, use
injectParameter(Class, String, Object).- Parameters:
params- The parameters to provide to the factory method.- Returns:
- The current
AssistedFactoryMockerinstance.
-
injectParameter
Injects a single parameter into the factory method.See
injectParameters(Object...)for more information.- Parameters:
param- The parameter to inject.- Returns:
- The current
AssistedFactoryMockerinstance.
-
injectParameter
SeeinjectParameter(Class, String, Object).Shortcut for unnamed parameters.
-
injectParameter
public <V> AssistedFactoryMocker<T,U> injectParameter(Class<V> type, @Nullable String name, V value) Sets the mocked instance of a constructor parameter.Only works for parameters that do not exist in the factory method.
- Type Parameters:
V- The type of the mocked object.- Parameters:
type- The type of the mocked object.name- The name of the mocked object, as specified byAssisted.value()in the constructor.value- The new mocked object to use as constructor parameter.- Returns:
- The current
AssistedFactoryMockerinstance.
-