Class ScriptedAction

  • All Implemented Interfaces:
    org.hamcrest.SelfDescribing, Action, Invokable

    public class ScriptedAction
    extends java.lang.Object
    implements Action

    An Action that executes a BeanShell script. This makes it easy to implement custom actions, especially those that call back to objects passed to the mocked method as parameters.

    To use a scripted action in an expectation, statically import the perform method and call it within the will(...) clause of the expectation.

    The script can refer to the parameters of the mocked method by the names $0 (the first parameter), $1, $2, etc, and to the mock object that has been invoked by the name $this. You can define other script variables by calling the action's where method.

    For example:

     allowing (sheep).accept(with(a(Visitor.class))); 
         will(perform("$0.visitSheep($this)");
     

    is equivalent to:

     allowing (sheep).accept(with(a(Visitor.class))); 
         will(perform("$0.visitSheep(s)").where("s", sheep);
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private bsh.Interpreter interpreter  
      private java.lang.String script  
    • Constructor Summary

      Constructors 
      Constructor Description
      ScriptedAction​(java.lang.String expression)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void defineParameters​(bsh.Interpreter interpreter, Invocation invocation)  
      void describeTo​(org.hamcrest.Description description)  
      java.lang.Object invoke​(Invocation invocation)
      Performs an action in response to an invocation.
      static ScriptedAction perform​(java.lang.String script)
      Creates an action that performs the given script.
      ScriptedAction where​(java.lang.String name, java.lang.Object value)
      Defines a variable that can be referred to by the script.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • interpreter

        private final bsh.Interpreter interpreter
      • script

        private final java.lang.String script
    • Constructor Detail

      • ScriptedAction

        public ScriptedAction​(java.lang.String expression)
    • Method Detail

      • invoke

        public java.lang.Object invoke​(Invocation invocation)
                                throws java.lang.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:
        java.lang.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.
      • defineParameters

        private void defineParameters​(bsh.Interpreter interpreter,
                                      Invocation invocation)
                               throws bsh.EvalError
        Throws:
        bsh.EvalError
      • describeTo

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

        public static ScriptedAction perform​(java.lang.String script)
        Creates an action that performs the given script.
        Parameters:
        script - a BeanShell script.
        Returns:
        the new action.
      • where

        public ScriptedAction where​(java.lang.String name,
                                    java.lang.Object value)
        Defines a variable that can be referred to by the script.
        Parameters:
        name - the name of the variable
        value - the value of the variable
        Returns:
        the action, so that more variables can be defined if needed