Best Jmock-library code snippet using org.jmock.lib.script.ScriptedAction.ScriptedAction
Source:ScriptedAction.java
...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 * 72 * @param script73 * a BeanShell script.74 * @return75 * the new action.76 */77 public static ScriptedAction perform(String script) {78 return new ScriptedAction(script);79 }80 81 /**82 * Defines a variable that can be referred to by the script.83 * 84 * @param name85 * the name of the variable86 * @param value87 * the value of the variable88 * @return89 * the action, so that more variables can be defined if needed90 */91 public ScriptedAction where(String name, Object value) {92 try {93 interpreter.set(name, value);94 }95 catch (EvalError e) {96 throw new IllegalArgumentException("invalid name binding", e);97 }98 return this;99 }100}...
Source:ScriptedActionTests.java
1package org.jmock.test.unit.lib.script;2import static org.hamcrest.Matchers.sameInstance;3import static org.jmock.lib.script.ScriptedAction.perform;4import junit.framework.TestCase;5import org.jmock.Expectations;6import org.jmock.Mockery;7import org.junit.Assert;8public class ScriptedActionTests extends TestCase {9 public interface Callout {10 void doSomethingWith(Callback cb) throws Exception;11 void doSomethingWithBoth(Callback cb1, Callback cb2);12 }13 14 public interface Callback {15 void callback();16 void throwException() throws Exception;17 void callbackWith(Object o);18 void callbackWith(Object o1, Object o2);19 }20 21 Mockery context = new Mockery();22 Callout callout = context.mock(Callout.class);...
ScriptedAction
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.Stub;5import org.jmock.lib.script.ScriptedAction;6{7 public void testScriptedAction()8 {9 Mock mock = mock(Stub.class);10 mock.expects(once()).method("invoke").with(eq("hello")).will(11 new ScriptedAction("return \"hello\";"));12 Invocation invocation = new Invocation("hello", new Object[] { "hello" }, 1);13 assertEquals("hello", ((Stub) mock.proxy()).invoke(invocation));14 }15}16import org.jmock.Mock;17import org.jmock.MockObjectTestCase;18import org.jmock.core.Invocation;19import org.jmock.core.Stub;20import org.jmock.lib.script.ScriptedAction;21{22 public void testScriptedAction()23 {24 Mock mock = mock(Stub.class);25 mock.expects(once()).method("invoke").with(eq("hello")).will(26 new ScriptedAction("return \"hello\";"));27 Invocation invocation = new Invocation("hello", new Object[] { "hello" }, 1);28 assertEquals("hello", ((Stub) mock.proxy()).invoke(invocation));29 }30}31import org.jmock.Mock;32import org.jmock.MockObjectTestCase;33import org.jmock.core.Invocation;34import org.jmock.core.Stub;35import org.jmock.lib.script.ScriptedAction;36{37 public void testScriptedAction()38 {39 Mock mock = mock(Stub.class);40 mock.expects(once()).method("invoke").with(eq("hello")).will(41 new ScriptedAction("return \"hello\";"));42 Invocation invocation = new Invocation("hello", new Object[] { "hello" }, 1);43 assertEquals("hello", ((Stub) mock.proxy()).invoke(invocation));44 }45}46import org.j
ScriptedAction
Using AI Code Generation
1package org.jmock.example.scriptedaction;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.Stub;5import org.jmock.core.stub.ReturnStub;6import org.jmock.core.stub.ThrowStub;7import org.jmock.example.scriptedaction.ScriptedActionExample;8import org.jmock.lib.script.Script;9import org.jmock.lib.script.ScriptedAction;10public class ScriptedActionTest extends MockObjectTestCase {11 public void testScriptedAction() {12 ScriptedActionExample example = new ScriptedActionExample();13 Script script = new Script();14 script.add(new ReturnStub("result 1"));15 script.add(new ReturnStub("result 2"));16 script.add(new ThrowStub(new RuntimeException("exception 1")));17 script.add(new ThrowStub(new RuntimeException("exception 2")));18 script.add(new ReturnStub("result 3"));19 script.add(new ReturnStub("result 4"));20 example.setScriptedAction(new ScriptedAction(script));21 assertEquals("result 1", example.method1());22 assertEquals("result 2", example.method1());23 try {24 example.method1();25 fail("Expected RuntimeException");26 } catch (RuntimeException e) {27 assertEquals("exception 1", e.getMessage());28 }29 try {30 example.method1();31 fail("Expected RuntimeException");32 } catch (RuntimeException e) {33 assertEquals("exception 2", e.getMessage());34 }35 assertEquals("result 3", example.method1());36 assertEquals("result 4", example.method1());37 }38}39package org.jmock.example.scriptedaction;40import org.jmock.MockObjectTestCase;41import org.jmock.core.Invocation;42import org.jmock.core.Stub;43import org.jmock.core.stub.ReturnStub;44import org.jmock.core.stub.ThrowStub;45import org.jmock.example.scriptedaction.ScriptedActionExample;46import org.jmock.lib.script.Script;47import org.jmock.lib.script.ScriptedAction;48public class ScriptedActionTest extends MockObjectTestCase {49 public void testScriptedAction() {50 ScriptedActionExample example = new ScriptedActionExample();51 Script script = new Script();52 script.add(new ReturnStub("result 1"));53 script.add(new ReturnStub("result 2"));54 script.add(new ThrowStub(new RuntimeException("
ScriptedAction
Using AI Code Generation
1import org.jmock.Mockery;2import org.jmock.Mock;3import org.jmock.lib.script.ScriptedAction;4import org.jmock.lib.script.ActionSequence;5import org.jmock.lib.script.ActionSequenceFactory;6public class ScriptedActionDemo {7 public static void main(String[] args) {8 Mockery context = new Mockery();9 Mock mock = context.mock(Interface.class);10 mock.expects(once()).method("method1").with(eq("Hello")).will(11 ScriptedAction.scriptedAction(12 new ActionSequence(13 ActionSequenceFactory.returnValue("Hello"),14 ActionSequenceFactory.returnValue("World")15 );16 Interface i = (Interface) mock.proxy();17 System.out.println(i.method1("Hello"));18 System.out.println(i.method1("Hello"));19 }20}21import org.jmock.Mockery;22import org.jmock.Mock;23import org.jmock.lib.script.ScriptedAction;24import org.jmock.lib.script.ActionSequence;25import org.jmock.lib.script.ActionSequenceFactory;26public class ScriptedActionDemo {27 public static void main(String[] args) {28 Mockery context = new Mockery();29 Mock mock = context.mock(Interface.class);30 mock.expects(once()).method("method1").with(eq("Hello")).will(31 ScriptedAction.scriptedAction(32 new ActionSequence(33 ActionSequenceFactory.returnValue("Hello"),34 ActionSequenceFactory.returnValue("World"),35 ActionSequenceFactory.throwException(new Exception("Error"))36 );37 Interface i = (Interface) mock.proxy();38 System.out.println(i.method1("Hello"));39 System.out.println(i.method1("Hello"));40 System.out.println(i.method1("Hello"));41 }42}43 at org.jmock.lib.script.ActionSequenceFactory.throwException(ActionSequenceFactory.java:30)44 at ScriptedActionDemo.main(ScriptedActionDemo.java:23)
ScriptedAction
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.script.ScriptedAction;4import org.jmock.lib.script.Script;5public class JMockScriptedActionTest extends MockObjectTestCase {6 private Mock mock;7 private ScriptedAction scriptedAction;8 protected void setUp() throws Exception {9 mock = mock(Interface.class);10 scriptedAction = new ScriptedAction();11 scriptedAction.setReturnValue("test");12 }13 public void testScriptedAction() {14 mock.expects(once()).method("method").will(scriptedAction);15 Interface i = (Interface) mock.proxy();16 assertEquals("test", i.method());17 }18 public void testScriptedActionWithScript() {19 Script script = new Script();20 script.addReturnValue("test1");21 script.addReturnValue("test2");22 scriptedAction.setScript(script);23 mock.expects(once()).method("method").will(scriptedAction);24 Interface i = (Interface) mock.proxy();25 assertEquals("test1", i.method());26 assertEquals("test2", i.method());27 }28 public void testScriptedActionWithScriptAndReturnValue() {29 Script script = new Script();30 script.addReturnValue("test1");31 script.addReturnValue("test2");32 scriptedAction.setScript(script);33 scriptedAction.setReturnValue("test3");34 mock.expects(once()).method("method").will(scriptedAction);35 Interface i = (Interface) mock.proxy();36 assertEquals("test1", i.method());37 assertEquals("test2", i.method());38 assertEquals("test3", i.method());39 }40 public void testScriptedActionWithScriptAndReturnValueAndThrow() {41 Script script = new Script();42 script.addReturnValue("test1");43 script.addReturnValue("test2");44 script.addThrow(new Exception());45 scriptedAction.setScript(script);46 scriptedAction.setReturnValue("test3");47 mock.expects(once()).method("method").will(scriptedAction);48 Interface i = (Interface) mock.proxy();49 assertEquals("test1", i.method());50 assertEquals("test2", i.method());51 try {52 i.method();53 fail("should have thrown an exception");54 } catch (Exception e) {55 }56 }57 public void testScriptedActionWithScriptAndThrow()
ScriptedAction
Using AI Code Generation
1Mock mockObject = new Mock(SomeInterface.class);2Method mockMethod = SomeInterface.class.getMethod("mockMethod", new Class[] {});3ScriptedAction scriptedAction = new ScriptedAction();4scriptedAction.addReturnValue(new Integer(1));5scriptedAction.addReturnValue(new Integer(2));6scriptedAction.addReturnValue(new Integer(3));7scriptedAction.addReturnValue(new Integer(4));8mockObject.expects(once()).method(mockMethod).will(scriptedAction);9SomeInterface object = (SomeInterface)mockObject.proxy();10assertEquals(1, object.mockMethod());11assertEquals(2, object.mockMethod());12assertEquals(3, object.mockMethod());13assertEquals(4, object.mockMethod());14assertEquals(4, object.mockMethod());15assertEquals(4, object.mockMethod());16mockObject.verify();
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!!