Class MockInjector<T>

java.lang.Object
nl.pim16aap2.testing.MockInjector<T>
Type Parameters:
T - The type of the class to create an instance of.

public class MockInjector<T> extends Object
Simple class that creates real instances of a class using its constructor that is annotated with Inject.

All arguments to the constructor are mocked instances of those classes.

  • Constructor Details

    • MockInjector

      public MockInjector(Class<T> clz) throws IllegalArgumentException
      Creates a new instance of this class.
      Parameters:
      clz - The class to create an instance of. This class must have a constructor annotated with Inject.
      Throws:
      IllegalArgumentException - If no constructor annotated with Inject is found.
  • Method Details

    • createInstance

      public T createInstance()
      Creates a new instance of the class using the constructor annotated with Inject.
      Returns:
      The new instance.
      Throws:
      RuntimeException - If an exception occurs while creating the instance.
    • injectInto

      public static <T> T injectInto(Class<T> clz) throws IllegalArgumentException, RuntimeException
      Creates a new instance of the class using the constructor annotated with Inject.
      Type Parameters:
      T - The type of the class to create an instance of.
      Parameters:
      clz - The class to create an instance of. This class must have a constructor annotated with Inject.
      Returns:
      The new instance.
      Throws:
      IllegalArgumentException - If no constructor annotated with Inject is found.
      RuntimeException - If an exception occurs while creating the instance.