|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.jmock.lib.script.ScriptedAction
public class ScriptedAction
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);
Constructor Summary | |
---|---|
ScriptedAction(String expression)
|
Method Summary | |
---|---|
void |
describeTo(Description description)
|
Object |
invoke(Invocation invocation)
Performs an action in response to an invocation. |
static ScriptedAction |
perform(String script)
Creates an action that performs the given script. |
ScriptedAction |
where(String name,
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 |
Constructor Detail |
---|
public ScriptedAction(String expression)
Method Detail |
---|
public Object invoke(Invocation invocation) throws Throwable
Invokable
invoke
in interface Invokable
invocation
- The invocation to perform.
null
if the invoked method has a void return type.
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.public void describeTo(Description description)
describeTo
in interface SelfDescribing
public static ScriptedAction perform(String script)
script
- a BeanShell script.
public ScriptedAction where(String name, Object value)
name
- the name of the variablevalue
- the value of the variable
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |