Best Jmock-library code snippet using org.jmock.lib.script.ScriptedAction.Interpreter
Source:ScriptedAction.java
2import org.hamcrest.Description;3import org.jmock.api.Action;4import org.jmock.api.Invocation;5import bsh.EvalError;6import bsh.Interpreter;7import bsh.TargetError;8/** 9 * <p>An {@link Action} that executes a <a href="http://www.beanshell.org">BeanShell</a> script.10 * This makes it easy to implement custom actions, especially those that call back to objects 11 * passed to the mocked method as parameters.</p>12 * 13 * <p>To use a scripted action in an expectation, statically import the {@link #perform(String) perform}14 * method and call it within the <code>will(...)</code> clause of the expectation.</p>15 * 16 * <p>The script can refer to the parameters of the mocked method by the names $0 (the first parameter), $1, $2, etc,17 * and to the mock object that has been invoked by the name $this.18 * You can define other script variables by calling the action's {@link #where(String, Object) where} method.</p>19 * 20 * <p>For example:</p>21 * <pre>22 * allowing (sheep).accept(with(a(Visitor.class))); 23 * will(perform("$0.visitSheep($this)");24 * </pre>25 * 26 * <p>is equivalent to:</p>27 * 28 * <pre>29 * allowing (sheep).accept(with(a(Visitor.class))); 30 * will(perform("$0.visitSheep(s)").where("s", sheep);31 * </pre>32 * 33 * 34 * @author nat35 *36 */37public class ScriptedAction implements Action {38 private final Interpreter interpreter = new Interpreter();39 private final String script;40 public ScriptedAction(String expression) {41 this.script = expression;42 this.interpreter.setStrictJava(true);43 }44 public Object invoke(Invocation invocation) throws Throwable {45 try {46 defineParameters(interpreter, invocation);47 return interpreter.eval(script);48 }49 catch (TargetError e) {50 throw e.getTarget();51 }52 catch (EvalError e) {53 throw new IllegalArgumentException("could not interpret script", e);54 }55 }56 57 private void defineParameters(Interpreter interpreter, Invocation invocation) 58 throws EvalError 59 {60 interpreter.set("$this", invocation.getInvokedObject());61 for (int i = 0; i < invocation.getParameterCount(); i++) {62 interpreter.set("$" + i, invocation.getParameter(i));63 }64 }65 public void describeTo(Description description) {66 description.appendText("perform ").appendText(script);67 }68 69 /**70 * Creates an action that performs the given script.71 * ...
Interpreter
Using AI Code Generation
1import org.jmock.lib.script.Interpreter;2import org.jmock.Mockery;3import org.jmock.Expectations;4import org.jmock.lib.script.ScriptedAction;5import org.jmock.lib.script.Script;6import java.io.File;7import java.io.FileWriter;8import java.io.IOException;9public class ScriptedActionTest {10 public static void main(String[] args) throws IOException {11 Mockery context = new Mockery();12 final Collaborator mock = context.mock(Collaborator.class);13 File scriptFile = new File("src/test/resources/scriptedActionTest.script");14 FileWriter fileWriter = new FileWriter(scriptFile);15 fileWriter.write("mock.doSomething(\"Hello World!\");");16 fileWriter.close();17 Script script = new Script(scriptFile);18 ScriptedAction scriptedAction = new ScriptedAction(script);19 context.checking(new Expectations()
Interpreter
Using AI Code Generation
1import org.jmock.lib.script.ScriptedAction;2import org.jmock.lib.script.Interpreter;3ScriptedAction action = new ScriptedAction();4Interpreter interpreter = new Interpreter();5action.setInterpreter(interpreter);6action.addScriptLine("System.out.println(\"Hello World!\");");7mockObject.expects(once()).method("method").will(action);8mockObject.method();9action.setInterpreter(interpreter);10action.addScriptLine("System.out.println(\"Hello World!\");");11mockObject.expects(once()).method("method").will(action);12mockObject.method();13ScriptedAction action = new ScriptedAction();14Interpreter interpreter = new Interpreter();15action.setInterpreter(interpreter);16action.addScriptLine("System.out.println(\"Hello World!\");");17mockObject.expects(once()).method("method").will(action);18mockObject.method();19ScriptedAction action = new ScriptedAction();20Interpreter interpreter = new Interpreter();21action.setInterpreter(interpreter);22action.addScriptLine("System.out.println(\"Hello World!\");");23mockObject.expects(once()).method("method").will(action);24mockObject.method();
Interpreter
Using AI Code Generation
1public class ScriptedActionTest {2 private Mockery context = new Mockery();3 private Mockery context2 = new Mockery();4 private Mockery context3 = new Mockery();5 private Mockery context4 = new Mockery();6 private Mockery context5 = new Mockery();7 private Mockery context6 = new Mockery();8 private Mockery context7 = new Mockery();9 private Mockery context8 = new Mockery();10 private Mockery context9 = new Mockery();11 private Mockery context10 = new Mockery();12 private Mockery context11 = new Mockery();13 private Mockery context12 = new Mockery();14 private Mockery context13 = new Mockery();15 private Mockery context14 = new Mockery();16 private Mockery context15 = new Mockery();17 private Mockery context16 = new Mockery();18 private Mockery context17 = new Mockery();19 private Mockery context18 = new Mockery();20 private Mockery context19 = new Mockery();21 private Mockery context20 = new Mockery();22 private Mockery context21 = new Mockery();23 private Mockery context22 = new Mockery();24 private Mockery context23 = new Mockery();25 private Mockery context24 = new Mockery();26 private Mockery context25 = new Mockery();27 private Mockery context26 = new Mockery();28 private Mockery context27 = new Mockery();29 private Mockery context28 = new Mockery();30 private Mockery context29 = new Mockery();31 private Mockery context30 = new Mockery();32 private Mockery context31 = new Mockery();33 private Mockery context32 = new Mockery();34 private Mockery context33 = new Mockery();
Interpreter
Using AI Code Generation
1import org.jmock.lib.script.ScriptedAction2import org.jmock.api.Invocation3def scriptedAction = new ScriptedAction()4scriptedAction.setLanguage('groovy')5scriptedAction.setScript('invocation.getInvokedObject().getSomething()')6def invocation = Mock(Invocation)7invocation.getInvokedObject() >> Mock(InvokedObject)8invocation.getInvokedObject().getSomething() >> 429assert scriptedAction.invoke(invocation) == 42
Interpreter
Using AI Code Generation
1actions.add(new ScriptedAction(new Interpreter()));2mocks.createMock("mock1", MyInterface.class);3mock1.method1("expected argument");4mock1.method1("actual argument");5mockery.assertIsSatisfied();6mocks.createMock("mock1", MyInterface.class);7mock1.method1("expected argument");8mock1.method1("unexpected argument");9mockery.assertIsSatisfied();10mocks.createMock("mock1", MyInterface.class);11mock1.method1("expected argument");12mock1.method1("unexpected argument");13mockery.assertIsSatisfied();14mocks.createMock("mock1", MyInterface.class);15mock1.method1("expected argument");16mock1.method1("unexpected argument");17mockery.assertIsSatisfied();18mocks.createMock("mock1", MyInterface.class);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!