Best Jmock-library code snippet using org.jmock.lib.script.ScriptedAction.where
Source:ScriptedAction.java
...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) {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
...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 }});...
where
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;6public class ScriptedActionTest extends MockObjectTestCase {7 public void testScriptedAction() {8 Mock mock = mock(Stub.class);9 mock.stubs().method("invoke").with(same(mock.proxy())).will(10 new ScriptedAction(11 "return new Integer(1);",12 "return new Integer(2);",13 "return new Integer(3);"14 );15 Stub stub = (Stub)mock.proxy();16 assertEquals(new Integer(1), stub.invoke(new Invocation(stub, "invoke", new Object[0])));17 assertEquals(new Integer(2), stub.invoke(new Invocation(stub, "invoke", new Object[0])));18 assertEquals(new Integer(3), stub.invoke(new Invocation(stub, "invoke", new Object[0])));19 }20}21import org.jmock.Mock;22import org.jmock.MockObjectTestCase;23import org.jmock.core.Invocation;24import org.jmock.core.Stub;25import org.jmock.lib.script.ScriptedAction;26public class ScriptedActionTest extends MockObjectTestCase {27 public void testScriptedAction() {28 Mock mock = mock(Stub.class);29 mock.stubs().method("invoke").with(same(mock.proxy())).will(30 new ScriptedAction(31 "return new Integer(1);",32 "return new Integer(2);",33 "return new Integer(3);"34 );35 Stub stub = (Stub)mock.proxy();36 assertEquals(new Integer(1), stub.invoke(new Invocation(stub, "invoke", new Object[0])));37 assertEquals(new Integer(2), stub.invoke(new Invocation(stub, "invoke", new Object[0])));38 assertEquals(new Integer(3), stub.invoke(new Invocation(stub, "invoke", new Object[0])));39 }40}
where
Using AI Code Generation
1package org.jmock.lib.script;2import org.jmock.core.*;3import org.jmock.core.constraint.*;4import org.jmock.core.matcher.*;5import org.jmock.core.stub.*;6import org.jmock.core.stub.ReturnStub;7import org.jmock.core.stub.ThrowStub;8import org.jmock.core.constraint.IsEqual;9import org.jmock.core.constraint.IsAnything;10import org.jmock.core.constraint.IsSame;11import org.jmock.core.constraint.IsNull;12import org.jmock.core.constraint.IsInstanceOf;13import org.jmock.core.constraint.IsNot;14import org.jmock.core.constraint.IsCollectionContaining;15import org.jmock.core.constraint.IsArrayContaining;16import org.jmock.core.constraint.IsArrayContainingInOrder;17import org.jmock.core.constraint.IsArrayContainingInAnyOrder;18import org.jmock.core.constraint.And;19import org.jmock.core.constraint.Or;20import org.jmock.core.constraint.Xor;21import org.jmock.core.constraint.Not;22import org.jmock.core.constraint.Is;23import org.jmock.core.constraint.IsNot;24import org.jmock.core.constraint.IsCompatibleType;25import org.jmock.core.constraint.IsEqual;26import org.jmock.core.constraint.IsIdentical;27import org.jmock.core.constraint.IsInstanceOf;28import org.jmock.core.constraint.IsSame;29import org.jmock.core.constraint.IsNull;30import org.jmock.core.constraint.IsAnything;31import org.jmock.core.constraint.IsArray;32import org.jmock.core.constraint.IsCollection;33import org.jmock.core.constraint.IsCollectionContaining;34import org.jmock.core.constraint.IsArrayContaining;35import org.jmock.core.constraint.IsArrayContainingInOrder;36import org.jmock.core.constraint.IsArrayContainingInAnyOrder;37import org.jmock.core.constraint.IsMapContaining;38import org.jmock.core.constraint.IsStringStarting;39import org.jmock.core.constraint.IsStringEnding;40import org.jmock.core.constraint.IsStringContaining;41import org.jmock.core.constraint.IsStringMatching;42import org.jmock.core.constraint.IsTypeCompatible;43import org.jmock.core.constraint.StringContains;44import org.jmock.core.constraint.IsEqualIgnoringCase;45import org.jmock.core.constraint.IsEqualComparingWhiteSpace;46import org.jmock.core.constraint.IsEqualTrimmingWhiteSpace;47import org.jmock.core.constraint.IsSame;48import org.jmock.core.constraint.IsInstanceOf;49import org.jmock.core.constraint.IsArrayContaining;50import org.jmock.core.constraint.IsCollectionContaining;51import org.jmock.core.constraint.IsMapContaining;52import org.jmock.core.constraint.IsStringStarting;53import org.jmock.core.constraint.IsStringEnding
where
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.core.constraint.IsEqual;6import org.jmock.core.matcher.InvokeOnceMatcher;7import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;8import org.jmock.core.matcher.InvokeAtMostOnceMatcher;9import org.jmock.core.matcher.InvokeAtLeastCountMatcher;10import org.jmock.core.matcher.InvokeAtMostCountMatcher;11import org.jmock.core.matcher.InvokeCountMatcher;12import org.jmock.core.matcher.InvokeBetweenCountMatcher;13import org.jmock.core.matcher.InvokeAllMatcher;14import org.jmock.core.matcher.InvokeAnyMatcher;15import org.jmock.core.matcher.InvokeOnceInOrderMatcher;16import org.jmock.core.matcher.InvokeAtLeastOnceInOrderMatcher;17import org.jmock.core.matcher.InvokeAtMostOnceInOrderMatcher;18import org.jmock.core.matcher.InvokeAtLeastCountInOrderMatcher;19import org.jmock.core.matcher.InvokeAtMostCountInOrderMatcher;20import org.jmock.core.matcher.InvokeCountInOrderMatcher;21import org.jmock.core.matcher.InvokeBetweenCountInOrderMatcher;22import org.jmock.core.matcher.InvokeAllInOrderMatcher;23import org.jmock.core.matcher.InvokeAnyInOrderMatcher;24import org.jmock.core.matcher.InvokeOnceInSequenceMatcher;25import org.jmock.core.matcher.InvokeAtLeastOnceInSequenceMatcher;26import org.jmock.core.matcher.InvokeAtMostOnceInSequenceMatcher;27import org.jmock.core.matcher.InvokeAtLeastCountInSequenceMatcher;28import org.jmock.core.matcher.InvokeAtMostCountInSequenceMatcher;29import org.jmock.core.matcher.InvokeCountInSequenceMatcher;30import org.jmock.core.matcher.InvokeBetweenCountInSequenceMatcher;31import org.jmock.core.matcher.InvokeAllInSequenceMatcher;32import org.jmock.core.matcher.InvokeAnyInSequenceMatcher;33import org.jmock.core.matcher.InvokeOnceInAnyOrderMatcher;34import org.jmock.core.matcher.InvokeAtLeastOnceInAnyOrderMatcher;35import org.jmock.core.matcher.InvokeAtMostOnceInAnyOrderMatcher;36import org.jmock.core.matcher.InvokeAtLeastCountInAnyOrderMatcher;37import org.jmock.core.matcher.InvokeAtMostCountInAnyOrderMatcher;38import org.jmock.core.matcher.InvokeCountInAnyOrderMatcher;39import org.jmock.core.matcher.InvokeBetweenCountInAnyOrderMatcher;40import org.jmock.core.matcher.InvokeAllInAnyOrderMatcher;41import org.jmock.core.matcher.InvokeAny
where
Using AI Code Generation
1import java.io.File;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.lib.script.ScriptedAction;6import org.jmock.util.ExpectedInvocation;7public class OneTest extends MockObjectTestCase {8 public void testOne() {9 Mock mock = mock(File.class);10 mock.expects(new ExpectedInvocation("exists", null, 1))11 .will(new ScriptedAction("return true;"));12 File file = (File) mock.proxy();13 assertTrue(file.exists());14 }15}16import java.io.File;17import org.jmock.Mock;18import org.jmock.MockObjectTestCase;19import org.jmock.core.Invocation;20import org.jmock.lib.script.ScriptedAction;21import org.jmock.util.ExpectedInvocation;22public class OneTest extends MockObjectTestCase {23 public void testOne() {24 Mock mock = mock(File.class);25 mock.expects(new ExpectedInvocation("exists", null, 1))26 .will(new ScriptedAction("return true;"));27 File file = (File) mock.proxy();28 assertTrue(file.exists());29 }30}31import java.io.File;32import org.jmock.Mock;33import org.jmock.MockObjectTestCase;34import org.jmock.core.Invocation;35import org.jmock.lib.script.ScriptedAction;36import org.jmock.util.ExpectedInvocation;37public class OneTest extends MockObjectTestCase {38 public void testOne() {39 Mock mock = mock(File.class);40 mock.expects(new ExpectedInvocation("exists", null, 1))41 .will(new ScriptedAction("return true;"));42 File file = (File) mock.proxy();43 assertTrue(file.exists());44 }45}46import java.io.File;47import org.jmock.Mock;48import org.jmock.MockObjectTestCase;49import org.jmock.core.Invocation;50import org.jmock.lib.script.ScriptedAction;51import org.jmock.util.ExpectedInvocation;52public class OneTest extends MockObjectTestCase {53 public void testOne() {54 Mock mock = mock(File.class);55 mock.expects(new ExpectedInvocation("exists", null
where
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Stub;4import org.jmock.core.StubSequence;5public class TestScriptedAction extends MockObjectTestCase {6 private Mock mock;7 private StubSequence stubSequence;8 private Stub stub;9 private ScriptedAction scriptedAction;10 public void setUp() {11 mock = mock(Stub.class, "mock");12 stubSequence = new StubSequence();13 stub = (Stub) mock.proxy();14 scriptedAction = new ScriptedAction(stubSequence);15 }16 public void testReturnsResultFromStub() {17 mock.expects(once()).method("invoke").with(eq("arg")).will(returnValue("result"));18 stubSequence.add(stub);19 assertEquals("result", scriptedAction.invoke("arg"));20 }21 public void testReturnsResultFromStubSequence() {22 mock.expects(once()).method("invoke").with(eq("arg")).will(returnValue("result"));23 stubSequence.add(stub);24 stubSequence.add(stub);25 scriptedAction.invoke("arg");26 assertEquals("result", scriptedAction.invoke("arg"));27 }28 public void testThrowsExceptionWhenNoMoreStubs() {29 mock.expects(once()).method("invoke").with(eq("arg")).will(returnValue("result"));30 stubSequence.add(stub);31 scriptedAction.invoke("arg");32 try {33 scriptedAction.invoke("arg");34 fail("should have thrown an exception");35 } catch (RuntimeException e) {36 assertEquals("no more stubs", e.getMessage());37 }38 }39}40import org.jmock.Mock;41import org.jmock.MockObjectTestCase;42import org.jmock.core.Stub;43import org.jmock.core.StubSequence;44public class TestScriptedAction extends MockObjectTestCase {45 private Mock mock;46 private StubSequence stubSequence;47 private Stub stub;
where
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.constraint.IsEqual;4import org.jmock.core.constraint.IsSame;5import org.jmock.core.constraint.IsInstanceOf;6import org.jmock.core.constraint.IsCollectionContaining;7import org.jmock.core.constraint.IsArrayContaining;8import org.jmock.core.constraint.IsAnything;9import org.jmock.core.constraint.IsNot;10import org.jmock.core.constraint.IsNotSame;11import org.jmock.core.cons
where
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.script.ScriptedAction;4public class TestScriptedAction extends MockObjectTestCase {5 public void testReturnsValuesInOrder() {6 Mock mock = mock(Interface.class);7 mock.stubs().method("doSomething").will(ScriptedAction.script(8 new Object[] { "foo", "bar", "baz" }));9 Interface i = (Interface) mock.proxy();10 assertEquals("foo", i.doSomething());11 assertEquals("bar", i.doSomething());12 assertEquals("baz", i.doSomething());13 }14}15interface Interface {16 public String doSomething();17}
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!!