Package nl.pim16aap2.testing
Class AssistedFactoryMocker<T,U>
java.lang.Object
nl.pim16aap2.testing.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.
Represents a class that can be used to create mocked objects for an
AssistedFactory.
Any dependencies that cannot be mocked will default to null. These can later be manually updated using
setMock(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) -
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> VGets the mocked object of the given type.<V> V<V> VbooleanChecks if the assisted factory mocker has a mocked object of the given type and with the given name.<V> AssistedFactoryMocker<T, U> Sets the mocked instance of a constructor parameter.<V> AssistedFactoryMocker<T, U>
-
Constructor Details
-
AssistedFactoryMocker
public AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass, org.mockito.MockSettings mockSettings) throws NoSuchMethodException - 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:
NoSuchMethodException- 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 NoSuchMethodException - 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:
NoSuchMethodException- If the factory method or the target constructor could not be found.
-
AssistedFactoryMocker
public AssistedFactoryMocker(Class<T> targetClass, Class<U> factoryClass) throws NoSuchMethodException - Parameters:
targetClass- The class that is instantiated by the factory.factoryClass- The factory class that instantiates the target class.- Throws:
NoSuchMethodException- If the factory method or the target constructor could not be found.
-
-
Method Details
-
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.
-
getMock
SeegetMock(boolean, Class, String).Shortcut for unnamed parameters.
-
getMock
-
getMock
@Contract("true, _, _ -> !null") public <V> V getMock(boolean nonNull, Class<V> type, @Nullable @Nullable String name) Gets the mocked object of the given type.- Type Parameters:
V- The type of the mocked object to get.- Parameters:
nonNull- Whether to allow null values. If this is true, an exception will be thrown in case the return value would be null.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.
-
hasMock
Checks if the assisted factory mocker has a mocked object of the given type and with the given name.- 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:
- True if a mocked object exists of the given type and name.
-
setMock
SeesetMock(Class, String, Object).Shortcut for unnamed parameters.
-
setMock
public <V> AssistedFactoryMocker<T,U> setMock(Class<V> type, @Nullable @Nullable String name, V mock) 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.mock- The new mocked object to use as constructor parameter.- Returns:
- The current
AssistedFactoryMockerinstance.
-