Best Jmock-library code snippet using org.jmock.lib.script.ScriptedAction.perform
Source:ScriptedAction.java
...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 * 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) {...
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);23 Callback callback = context.mock(Callback.class);24 Callback callback2 = context.mock(Callback.class, "callback2");25 26 27 public void testInterpretsCallbackExpression() throws Exception {28 context.checking(new Expectations() {{29 oneOf (callout).doSomethingWith(callback); will(perform("$0.callback()"));30 oneOf (callback).callback();31 }});32 33 callout.doSomethingWith(callback);34 35 context.assertIsSatisfied();36 }37 public void testScriptCanReferToParametersByIndex() {38 context.checking(new Expectations() {{39 oneOf (callout).doSomethingWithBoth(callback, callback2); will(perform("$1.callback()"));40 oneOf (callback2).callback();41 }});42 43 callout.doSomethingWithBoth(callback, callback2);44 45 context.assertIsSatisfied();46 }47 48 public void testScriptCanReferToInvokedObjectAs$This() throws Exception {49 context.checking(new Expectations() {{50 oneOf (callout).doSomethingWith(callback); will(perform("$0.callbackWith($this)"));51 oneOf (callback).callbackWith(callout);52 }});53 54 callout.doSomethingWith(callback);55 56 context.assertIsSatisfied();57 }58 59 public void testCanBindValuesFromTestIntoScript() throws Exception {60 final Object v1 = new Object();61 final Object v2 = new Object();62 63 context.checking(new Expectations() {{64 oneOf (callout).doSomethingWith(callback); will(perform("$0.callbackWith(x, y)").where("x", v1).where("y", v2));65 oneOf (callback).callbackWith(v1, v2);66 }});67 68 callout.doSomethingWith(callback);69 70 context.assertIsSatisfied();71 }72 public void testExceptionThrownByCallbackIsPassedBackToCaller() throws Exception {73 final Exception exception = new Exception("exception");74 75 context.checking(new Expectations() {{76 oneOf (callout).doSomethingWith(callback); will(perform("$0.throwException()"));77 oneOf (callback).throwException(); will(throwException(exception));78 }});79 80 try {81 callout.doSomethingWith(callback);82 }83 catch (Exception e) {84 Assert.assertThat(e, sameInstance(exception));85 }86 87 context.assertIsSatisfied();88 }89}...
perform
Using AI Code Generation
1package org.jmock.test.acceptance;2import junit.framework.TestCase;3import org.jmock.Mock;4import org.jmock.MockObjectTestCase;5import org.jmock.core.Invocation;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.Stub;8import org.jmock.core.constraint.IsEqual;9import org.jmock.core.constraint.IsAnything;10import org.jmock.core.constraint.IsSame;11import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;12import org.jmock.core.matcher.InvokeOnceMatcher;13import org.jmock.core.matcher.InvokeTimesMatcher;14import org.jmock.core.matcher.InvokeWithAnyArgumentsMatcher;15import org.jmock.core.matcher.InvokeWithMatcher;16import org.jmock.core.matcher.TestFailureMatcher;17import org.jmock.core.stub.ConsecutiveStub;18import org.jmock.core.stub.DoAllStub;19import org.jmock.core.stub.DoNothingStub;20import org.jmock.core.stub.DoNothingStub;21import org.jmock.core.stub.DoReturnStub;22import org.jmock.core.stub.DoThrowStub;
perform
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.script.ScriptedAction;4{5 public void testScriptedAction() throws Exception6 {7 Mock mock = mock(Interface.class);8 mock.expects(once()).method("perform").will(new ScriptedAction("perform"));9 Interface i = (Interface)mock.proxy();10 i.perform();11 }12}13{14 public void perform();15}
perform
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.InvocationMatcher;5import org.jmock.core.Stub;6import org.jmock.core.constraint.IsAnything;7import org.jmock.core.constraint.IsEqual;8import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;9import org.jmock.lib.script.ScriptedAction;10import org.jmock.lib.script.ScriptedStub;11public class ScriptedActionTest extends MockObjectTestCase {12 public void testScriptedAction() {13 Mock mock = mock(InvocationMatcher.class);14 mock.expects(once()).method("matches").with(eq("foo")).will(15 returnValue(true));16 mock.expects(once()).method("invoked").with(eq("foo"));17 ScriptedAction action = new ScriptedAction();18 action.addLine("return \"bar\";");19 action.addLine("mock.invoked(invocation);");20 Object result = action.invoke(21 new Invocation("foo", null, null, null, null, null),22 new IsEqual("foo"),23 mock.proxy());24 assertEquals("bar", result);25 }26 public void testScriptedStub() {27 Mock mock = mock(Stub.class);28 mock.expects(once()).method("invoke").with(eq("foo")).will(29 returnValue("bar"));30 ScriptedStub stub = new ScriptedStub();31 stub.addLine("return \"bar\";");32 Object result = stub.invoke(33 new Invocation("foo", null, null, null, null, null),34 new IsEqual("foo"),35 mock.proxy());36 assertEquals("bar", result);37 }38}39import org.jmock.Mock;40import org.jmock.MockObjectTestCase;41import org.jmock.core.Invocation;42import org.jmock.core.InvocationMatcher;43import org.jmock.core.Stub;44import org.jmock.core.constraint.IsAnything;45import org.jmock.core.constraint.IsEqual;46import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;47import org.jmock.lib.script.ScriptedAction;48import org.jmock.lib.script.ScriptedStub;49public class ScriptedActionTest extends MockObjectTestCase {50 public void testScriptedAction() {51 Mock mock = mock(
perform
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 Mock mock = new Mock(Interface.class);4 mock.expects(once()).method("method1").will(5 new ScriptedAction("method1", new Object[] {6 new Integer(1), new Integer(2)7 }, new Object[] {8 new Integer(3), new Integer(4)9 })10 );11 Interface i = (Interface)mock.proxy();12 int result = i.method1(1, 2);13 System.out.println("result = " + result);14 }15}16public class 2 {17 public static void main(String[] args) {18 Mock mock = new Mock(Interface.class);19 mock.expects(once()).method("method1").will(20 new ScriptedAction("method1", new Object[] {21 new Integer(1), new Integer(2)22 }, new Object[] {23 new Integer(3), new Integer(4)24 })25 );26 Interface i = (Interface)mock.proxy();27 int result = i.method1(1, 2);28 System.out.println("result = " + result);29 }30}31public class 3 {32 public static void main(String[] args) {33 Mock mock = new Mock(Interface.class);34 mock.expects(once()).method("method1").will(35 new ScriptedAction("method1", new Object[] {36 new Integer(1), new Integer(2)37 }, new Object[] {38 new Integer(3), new Integer(4)39 })40 );41 Interface i = (Interface)mock.proxy();42 int result = i.method1(1, 2);43 System.out.println("result = " + result);44 }45}46public class 4 {47 public static void main(String[] args) {48 Mock mock = new Mock(Interface.class);49 mock.expects(once()).method("method1").will(50 new ScriptedAction("method1", new Object[] {51 new Integer(1), new Integer(2)
perform
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 ScriptedAction sa = new ScriptedAction();4 sa.perform("script.txt");5 Class1 c = new Class1();6 c.method1(sa.value);7 }8}9value = "Hello World";10public class Class1 {11 public void method1(String s) {12 System.out.println(s);13 }14}15public class 2 {16 public static void main(String[] args) {17 ScriptedAction sa = new ScriptedAction();18 sa.perform("script.txt");19 Class2 c = new Class2();20 c.method2(sa.value);21 }22}23value = 10;24public class Class2 {25 public void method2(int i) {26 System.out.println(i);27 }28}29public class 3 {30 public static void main(String[] args) {31 ScriptedAction sa = new ScriptedAction();32 sa.perform("script.txt");33 Class3 c = new Class3();34 c.method3(sa.value);35 }36}37value = "Hello World";
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!!