Class ReturnDefaultValueAction

java.lang.Object
org.jmock.internal.ReturnDefaultValueAction
All Implemented Interfaces:
org.hamcrest.SelfDescribing, Action, Invokable

public class ReturnDefaultValueAction extends Object implements Action
Returns a default value for the invoked method's result type.
  • Returns nothing from void methods.
  • Returns zero or false results for primitive types.
  • Returns zero length instances for arrays and strings.
  • Returns empty instances for collections and maps types in java.util.
  • Returns imposterised Null Objects for types that can be imposterised by the action's Imposteriser.
  • Otherwise returns null.
The default value can be overridden for specific types.
  • Field Details

    • CONCRETE_COLLECTION_TYPES

      private static final Class<?>[] CONCRETE_COLLECTION_TYPES
    • resultValuesByType

      private final Map<Class<?>,Object> resultValuesByType
    • imposteriser

      private Imposteriser imposteriser
  • Constructor Details

    • ReturnDefaultValueAction

      public ReturnDefaultValueAction(Imposteriser imposteriser, Map<Class<?>,Object> typeToResultValue)
    • ReturnDefaultValueAction

      public ReturnDefaultValueAction(Imposteriser imposteriser)
    • ReturnDefaultValueAction

      public ReturnDefaultValueAction()
  • Method Details

    • setImposteriser

      public void setImposteriser(Imposteriser newImposteriser)
    • describeTo

      public void describeTo(org.hamcrest.Description description)
      Specified by:
      describeTo in interface org.hamcrest.SelfDescribing
    • addResult

      public void addResult(Class<?> resultType, Object resultValue)
    • invoke

      public Object invoke(Invocation invocation) throws Throwable
      Description copied from interface: Invokable
      Performs an action in response to an invocation.
      Specified by:
      invoke in interface Invokable
      Parameters:
      invocation - The invocation to perform.
      Returns:
      The result of the invocation, if not throwing an exception. Must return null if the invoked method has a void return type.
      Throws:
      Throwable - An exception to be thrown to the caller, if not returning a value. Any checked exception thrown must be in the throws list of the invoked method.
    • collectionOrMapInstanceFor

      private static Object collectionOrMapInstanceFor(Class<?> returnType) throws Throwable
      Throws:
      Throwable
    • instanceForCollectionType

      private static Object instanceForCollectionType(Class<?> type) throws Throwable
      Throws:
      Throwable
    • isCollectionOrMap

      private static boolean isCollectionOrMap(Class<?> type)
    • cannotCreateNewInstance

      private static boolean cannotCreateNewInstance(Class<?> returnType)
    • createDefaultResults

      protected static Map<Class<?>,Object> createDefaultResults()