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.

public class AssistedFactoryMocker<T,U> extends Object
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 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

      public U 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

      public <V> V getMock(Class<V> type)
      See getMock(boolean, Class, String).

      Shortcut for unnamed parameters.

    • getMock

      public <V> V getMock(Class<V> type, @Nullable @Nullable String name)
    • 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 by Assisted.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

      public boolean hasMock(Class<?> type, @Nullable @Nullable String name)
      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 by Assisted.value() in the constructor.
      Returns:
      True if a mocked object exists of the given type and name.
    • setMock

      public <V> AssistedFactoryMocker<T,U> setMock(Class<V> type, V mock)
      See setMock(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 by Assisted.value() in the constructor.
      mock - The new mocked object to use as constructor parameter.
      Returns:
      The current AssistedFactoryMocker instance.