Class CommonsMock


  • public final class CommonsMock
    extends Object
    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
    • Constructor Detail

      • CommonsMock

        public CommonsMock()
    • Method Detail

      • registerStaticConstant

        public static <T> void registerStaticConstant​(@Nonnull
                                                      T aObject)
        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 be null .
      • registerStatic

        public static <T> void registerStatic​(@Nonnull
                                              Class<T> aClass,
                                              @Nonnull
                                              Supplier<T> aSupplier)
        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 be null.
        aSupplier - The supplier/factory to be invoked when to mock this class. May not be null.
      • 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 be null.
        aParams - The parameter declarations to be used. May not be null.
        aSupplier - The generic function to be invoked. Must take an array of IGetterDirectTrait and 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 be null.
      • registerPerInstanceConstant

        public <T> void registerPerInstanceConstant​(@Nonnull
                                                    T aObject)
        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 be null .
      • registerPerInstance

        public <T> void registerPerInstance​(@Nonnull
                                            Class<T> aClass,
                                            @Nonnull
                                            Supplier<T> aSupplier)
        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 be null.
        aSupplier - The supplier/factory to be invoked when to mock this class. May not be null.
      • 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 be null.
        aParams - The parameter declarations to be used. May not be null.
        aSupplier - The generic function to be invoked. Must take an array of IGetterDirectTrait and 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 be null.
      • mock

        @Nonnull
        public <T> T mock​(@Nonnull
                          Class<T> aClass,
                          @Nullable
                          Object... aParams)
        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 be null.
        aParams - An optional array of parameters to be passed to the mocking supplier. May be null or 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 a List of 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 be null or empty.
        Returns:
        The list with nCount entries.
      • mockSet

        @Nonnull
        @ReturnsMutableCopy
        public <T> ICommonsSet<T> mockSet​(@Nonnegative
                                          int nCount,
                                          @Nonnull
                                          Class<T> aClass,
                                          @Nullable
                                          Object... aParams)
        Create a ICommonsSet of 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 be null or empty.
        Returns:
        The set with nCount entries.