Best Jmock-library code snippet using org.jmock.lib.script.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.Constraint;4import org.jmock.core.constraint.IsAnything;5import org.jmock.core.constraint.IsEqual;6import org.jmock.core.constraint.IsInstanceOf;7import org.jmock.core.constraint.IsSame;8import org.jmock.core.constraint.IsStringStarting;9import org.jmock.core.constraint.IsStringEnding;10import org.jmock.core.constraint.IsStringContaining;11import org.jmock.core.constraint.IsLessThan;12import org.jmock.core.constraint.IsGreaterThan;13import org.jmock.core.constraint.IsIn;14import org.jmock.core.constraint.IsNot;15import org.jmock.core.constraint.IsNotSame;16import org.jmock.core.constraint.IsNotIn;17import org.jmock.core.constraint.IsCollectionContaining;18import org.jmock.core.constraint.IsMapContaining;19import org.jmock.core.constraint.IsArrayContaining;20import org.jmock.core.constraint.IsArrayContainingInOrder;21import org.jmock.core.constraint.StringContains;22import org.jmock.core.constraint.StringEndsWith;23import org.jmock.core.constraint.StringStartsWith;24import org.jmock.core.constraint.StringMatches;25import org.jmock.core.constraint.IsEqualIgnoringCase;26import org.jmock.core.constraint.IsArrayEqual;27import org.jmock.core.constraint.IsCollectionEqual;28import org.jmock.core.constraint.IsMapEqual;29import org.jmock.core.constraint.IsEqualComparing;30import org.jmock.core.constraint.IsEqualComparingFields;31import org.jmock.core.constraint.IsEqualComparingPrivateFields;32import org.jmock.core.constraint.IsEqualComparingPublicFields;33import org.jmock.core.constraint.IsEqualComparingToString;34import org.jmock.core.constraint.IsEqualIncluding;35import org.jmock.core.constraint.IsEqualIncludingFields;36import org.jmock.core.constraint.IsEqualIncludingPrivateFields;37import org.jmock.core.constraint.IsEqualIncludingPublicFields;38import org.jmock.core.constraint.IsEqualIncludingToString;39import org.jmock.core.constraint.IsEqualIgnoringFields;40import org.jmock.core.constraint.IsEqualIgnoringPrivateFields;41import org.jmock.core.constraint.IsEqualIgnoringPublicFields;42import org.jmock.core.constraint.IsEqualIgnoringToString;43import org.jmock.core.constraint.IsEqualIgnoringWhitespace;44import org.jmock.core.constraint.IsEqualTrimmingWhitespace;45import org.jmock.core.constraint.IsEqualWithDelta;46import org.jmock.core.constraint.IsSameInstance;47import org.jmock.core.constraint.IsSameClass;48import org.jmock.core.constraint.IsSameOrSubclass;49import org.jmock
ScriptedAction
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.Constraint;4import org.jmock.core.constraint.IsAnything;5import org.jmock.core.constraint.IsEqual;6import org.jmock.core.constraint.IsNot;7import org.jmock.core.constraint.IsSame;8import org.jmock.core.constraint.IsInstanceOf;9import org.jmock.core.constraint.IsCollectionContaining;10import org.jmock.core.constraint.IsStringStarting;11import org.jmock.core.constraint.IsStringEnding;12import org.jmock.core.constraint.IsStringContaining;13import org.jmock.core.constraint.IsStringMatching;14import org.jmock.core.constraint.IsIn;15import org.jmock.core.constraint.AllOf;16import org.jmock.core.constraint.AnyOf;17import org.jmock.core.constraint.Not;18import org.jmock.core.constraint.StringContains;19import org.jmock.core.constraint.IsEqualIgnoringCase;20import org.jmock.core.constraint.IsEqualComparingWhiteSpace;21import org.jmock.core.constraint.IsMapContaining;22import org.jmock.core.constraint.IsArrayContaining;23import org.jmock.core.constraint.IsArrayContainingInOrder;24import org.jmock.core.constraint.IsArrayContainingInAnyOrder;25import org.jmock.core.constraint.IsArrayWithSize;26import org.jmock.core.constraint.IsCollectionWithSize;27import org.jmock.core.constraint.IsMapWithSize;28import org.jmock.core.constraint.IsComparable;29import org.jmock.core.constraint.IsLessThan;30import org.jmock.core.constraint.IsGreaterThan;31import org.jmock.core.constraint.IsLessThanOrEqualTo;32import org.jmock.core.constraint.IsGreaterThanOrEqualTo;33import org.jmock.core.constraint.IsBetween;34import org.jmock.core.constraint.IsCloseTo;35import org.jmock.core.constraint.IsInstanceOf;36import org.jmock.core.constraint.IsSame;37import org.jmock.core.constraint.IsEqual;38import org.jmock.core.constraint.IsAnything;39import org.jmock.core.constraint.IsNot;40import org.jmock.core.constraint.IsSame;41import org.jmock.core.constraint.IsInstanceOf;42import org.jmock.core.constraint.IsCollectionContaining;43import org.jmock.core.constraint.IsStringStarting;44import org.jmock.core.constraint.IsStringEnding;45import org.jmock.core.constraint.IsStringContaining;46import org.jmock.core.constraint.IsStringMatching;47import org.jmock.core.constraint.IsIn;48import org.jmock.core.constraint.AllOf;49import org.jmock.core.constraint.AnyOf;50import org.jmock.core.constraint.Not;51import org.jmock.core.constraint.StringContains;52import org.jmock.core.constraint.IsEqualIgnoringCase;53import org
ScriptedAction
Using AI Code Generation
1package com.jmock.test;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.core.InvocationMatcher;6import org.jmock.core.Stub;7import org.jmock.core.constraint.IsEqual;8import org.jmock.core.constraint.IsInstanceOf;9import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;10import org.jmock.core.matcher.InvokeOnceMatcher;11import org.jmock.core.matcher.InvokeRecorder;12import org.jmock.core.matcher.TestFailureMatcher;13import org.jmock.core.stub.ReturnStub;14import org.jmock.core.stub.ThrowStub;15import org.jmock.core.stub.VoidStub;16import org.jmock.lib.script.ScriptedAction;17import org.jmock.util.Dummy;18import org.jmock.util.DummyFactory;19import org.jmock.util.Verifier;20public class TestScriptedAction extends MockObjectTestCase {21 private Mock mockAction;22 private ScriptedAction scriptedAction;23 private Invocation invocation;24 public void setUp() {25 mockAction = mock(Stub.class);26 scriptedAction = new ScriptedAction();27 invocation = new Invocation("INVOKED-OBJECT", "INVOKED-METHOD", new Object[0], new Class[0]);28 }29 public void testCanAddActions() {30 scriptedAction.add((Stu
ScriptedAction
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.lib.script.ScriptedAction;3import org.jmock.Mock;4import org.jmock.core.Invocation;5import org.jmock.core.InvocationMatcher;6import org.jmock.core.Stub;7import org.jmock.core.constraint.IsEqual;8import org.jmock.core.constraint.IsAnything;9import org.jmock.core.constraint.IsSame;10import org.jmock.core.matcher.InvokeOnceMatcher;11import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;12import org.jmock.core.matcher.InvokeAtMostOnceMatcher;13import org.jmock.core.matcher.InvokeAtLeastCountMatcher;14import org.jmock.core.matcher.InvokeAtMostCountMatcher;15import org.jmock.core.matcher.InvokeCountMatcher;16import org.jmock.core.matcher.InvokeBetweenCountMatcher;17import org.jmock.core.matcher.InvokeBetweenInclusiveCountMatcher;18import org.jmock.core.matcher.InvokeExactlyCountMatcher;19import org.jmock.core.matcher.InvokeIdempotentMatcher;20import org.jmock.core.matcher.InvokeIdempotentAtLeastOnceMatcher;21import org.jmock.core.matcher.InvokeIdempotentAtMostOnceMatcher;22import org.jmock.core.matcher.InvokeIdempotentAtLeastCountMatcher;23import org.jmock.core.matcher.InvokeIdempotentAtMostCountMatcher;24import org.jmock.core.matcher.InvokeIdempotentCountMatcher;25import org.jmock.core.matcher.InvokeIdempotentBetweenCountMatcher;26import org.jmock.core.matcher.InvokeIdempotentBetweenInclusiveCountMatcher;27import org.jmock.core.matcher.InvokeIdempotentExactlyCountMatcher;28import org.jmock.core.matcher.InvokeAtLeastOnceInOrderMatcher;29import org.jmock.core.matcher.InvokeAtLeastCountInOrderMatcher;30import org.jmock.core.matcher.InvokeAtMostOnceInOrderMatcher;31import org.jmock.core.matcher.InvokeAtMostCountInOrderMatcher;32import org.jmock.core.matcher.InvokeCountInOrderMatcher;33import org.jmock.core.matcher.InvokeBetweenCountInOrderMatcher;34import org.jmock.core.matcher.InvokeBetweenInclusiveCountInOrderMatcher;35import org.jmock.core.matcher.InvokeExactlyCountInOrderMatcher;36import org.jmock.core.matcher.InvokeIdempotentAtLeastOnceInOrderMatcher;37import org.jmock.core.matcher.InvokeIdempotentAtLeastCountInOrderMatcher;38import org.jmock.core.matcher.InvokeIdempotentAtMostOnceInOrderMatcher;39import org.jmock.core.matcher.InvokeIdempotentAtMostCountInOrderMatcher;40import org.jmock.core.matcher.InvokeIdempotentCountInOrder
ScriptedAction
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.script.ScriptedAction;4public class TestScriptedAction extends MockObjectTestCase {5 private Mock mock;6 private ScriptedAction action;7 private Object[] args;8 private Object[] result;9 public void setUp() {10 mock = mock(Runnable.class);11 action = new ScriptedAction();12 args = new Object[] { "arg1", "arg2" };13 result = new Object[] { "result1", "result2" };14 }15 public void testReturnsResultFromScript() {16 action.addResult(result[0]);17 action.addResult(result[1]);18 assertEquals("result1", action.invoke(mock.proxy(), args));19 assertEquals("result2", action.invoke(mock.proxy(), args));20 }21 public void testThrowsExceptionFromScript() {22 action.addException(new RuntimeException("exception1"));23 action.addException(new RuntimeException("exception2"));24 try {25 action.invoke(mock.proxy(), args);26 fail("Should have thrown exception");27 } catch (RuntimeException e) {28 assertEquals("exception1", e.getMessage());29 }30 try {31 action.invoke(mock.proxy(), args);32 fail("Should have thrown exception");33 } catch (RuntimeException e) {34 assertEquals("exception2", e.getMessage());35 }36 }37 public void testThrowsExceptionIfNoResultOrException() {38 try {39 action.invoke(mock.proxy(), args);40 fail("Should have thrown exception");41 } catch (RuntimeException e) {42 assertEquals("No result or exception defined in script", e.getMessage());43 }44 }45 public void testThrowsExceptionIfNoResultOrExceptionAfterReturningResult() {46 action.addResult(result[0]);47 assertEquals("result1", action.invoke(mock.proxy(), args));48 try {49 action.invoke(mock.proxy(), args);50 fail("Should have thrown exception");51 } catch (RuntimeException e) {52 assertEquals("No result or exception defined in script", e.getMessage());53 }54 }55 public void testThrowsExceptionIfNoResultOrExceptionAfterThrowingException() {56 action.addException(new RuntimeException("exception1"));57 try {58 action.invoke(mock.proxy(), args);59 fail("Should have thrown exception");60 } catch (RuntimeException e) {61 assertEquals("exception1", e.getMessage());62 }63 try {64 action.invoke(mock.proxy(), args);65 fail("Should have
ScriptedAction
Using AI Code Generation
1package org.jmock.examples;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Constraint;5import org.jmock.core.constraint.IsEqual;6import org.jmock.core.constraint.IsSame;7import org.jmock.core.stub.ReturnStub;8import org.jmock.core.stub.ThrowStub;9import org.jmock.lib.script.ScriptedAction;10public class ScriptedActionTest extends MockObjectTestCase {11 public void testScriptedAction() throws Exception {12 Mock mock = mock(Foo.class);13 mock.stubs().method("doSomething").with(isA(String.class))14 .will(new ScriptedAction(new Object[]{15 new IsEqual("first"),16 new ReturnStub("first result"),17 new IsEqual("second"),18 new ReturnStub("second result"),19 new IsEqual("third"),20 new ReturnStub("third result"),21 new IsEqual("fourth"),22 new ReturnStub("fourth result"),23 new IsEqual("fifth"),24 new ReturnStub("fifth result"),25 new IsEqual("sixth"),26 new ReturnStub("sixth result"),27 new IsEqual("seventh"),28 new ReturnStub("seventh result"),29 new IsEqual("eighth"),30 new ReturnStub("eighth result"),31 new IsEqual("ninth"),32 new ReturnStub("ninth result"),33 new IsEqual("tenth"),34 new ReturnStub("tenth result"),35 new IsEqual("eleventh"),36 new ReturnStub("eleventh result"),37 new IsEqual("twelfth"),38 new ReturnStub("twelfth result"),39 new IsEqual("thirteenth"),40 new ReturnStub("thirteenth result"),41 new IsEqual("fourteenth"),42 new ReturnStub("fourteenth result"),43 new IsEqual("fifteenth"),44 new ReturnStub("fifteenth result"),45 new IsEqual("sixteenth"),46 new ReturnStub("sixteenth result"),47 new IsEqual("seventeenth"),48 new ReturnStub("seventeenth result"),49 new IsEqual("eighteenth"),50 new ReturnStub("eighteenth result"),51 new IsEqual("nineteenth"),52 new ReturnStub("nineteenth result"),53 new IsEqual("twentieth"),54 new ReturnStub("twentieth result"),55 new IsEqual("twenty-first"),56 new ReturnStub("twenty-first result"),
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;5import org.jmock.lib.script.ActionSequence;6import org.jmock.lib.script.ActionSequenceException;7import org.jmock.lib.script.ActionSequenceVerificationError;8import org.jmock.core.Invocation;9import org.jmock.core.Stub;10import org.jmock.core.Verifiable;11import org.jmock.core.VerifiableList;12import org.jmock.core.StubVerifiableList;13import org.jmock.core.InvocationMatcher;14import org.jmock.core.Constraint;15import org.jmock.core.constraint.IsAnything;16import org.jmock.core.constraint.IsEqual;17import org.jmock.core.constraint.IsSame;18import org.jmock.core.constraint.IsInstanceOf;19import org.jmock.core.constraint.IsIn;20import org.jmock.core.constraint.IsNull;21import org.jmock.core.constraint.IsNot;22import org.jmock.core.constraint.IsCollectionContaining;23import org.jmock.core.constraint.IsArrayContaining;24import org.jmock.core.constraint.IsArrayContainingInOrder;25import org.jmock.core.constraint.IsArrayNotContaining;26import org.jmock.core.constraint.IsArrayNotContainingInOrder;27import org.jmock.core.constraint.IsCollectionNotContaining;28import org.jmock.core.constraint.IsCollectionOrdered;29import org.jmock.core.constraint.IsCollectionUnordered;30import org.jmock.core.constraint.IsStringContaining;31import org.jmock.core.constraint.IsStringNotContaining;32import org.jmock.core.constraint.IsStringStarting;33import org.jmock.core.constraint.IsStringEnding;34import org.jmock.core.constraint.IsStringMatching;35import org.jmock.core.constraint.Is;36import org.jmock.core.constraint.IsNot;37import org.jmock.core.constraint.IsCompatibleType;38import org.jmock.core.constraint.IsSubtype;39import org.jmock.core.constraint.IsInstanceOf;40import org.jmock.core.constraint.IsSame;41import org.jmock.core.constraint.IsEqual;42import org.jmock.core.constraint.IsAnything;43import org.jmock.core.constraint.IsArrayContaining;44import org.jmock.core.constraint.IsArrayContainingInOrder;45import org.jmock.core.constraint.IsArrayNotContaining;46import org.jmock.core.constraint.IsArrayNotContainingInOrder;47import org.jmock.core.constraint.IsCollectionContaining;48import org.jmock.core.constraint.IsCollectionNotContaining;49import org.jmock.core.constraint.IsCollectionOrdered;50import org.jmock.core.constraint.IsCollectionUnordered;
ScriptedAction
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.lib.script.ScriptedAction;4import org.jmock.lib.script.ActionScript;5{6 public void testScriptedAction()7 {8 Mock mock = mock(Interface.class);9 mock.expects(once()).method("method1").will(returnValue("Hello"));10 mock.expects(once()).method("method1").will(returnValue("World"));11 mock.expects(once()).method("method1").will(returnValue("JMock"));12 mock.expects(once()).method("method1").will(returnValue("ScriptedAction"));13 Interface i = (Interface)mock.proxy();14 assertEquals("Hello", i.method1());15 assertEquals("World", i.method1());16 assertEquals("JMock", i.method1());17 assertEquals("ScriptedAction", i.method1());18 }19}20import org.jmock.Mock;21import org.jmock.MockObjectTestCase;22import org.jmock.lib.script.ScriptedAction;23import org.jmock.lib.script.ActionScript;24{25 public void testScriptedAction()26 {27 Mock mock = mock(Interface.class);28 mock.expects(once()).method("method1").will(returnValue("Hello"));29 mock.expects(once()).method("method1").will(returnValue("World"));30 mock.expects(once()).method("method1").will(returnValue("JMock"));31 mock.expects(once()).method("method1").will(returnValue("ScriptedAction"));32 Interface i = (Interface)mock.proxy();33 assertEquals("Hello", i.method1());34 assertEquals("World", i.method1());35 assertEquals("JMock", i.method1());36 assertEquals("ScriptedAction", i.method1());37 }38}
ScriptedAction
Using AI Code Generation
1import junit.framework.TestCase;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.core.InvocationMatcher;6import org.jmock.core.constraint.IsAnything;7import org.jmock.core.constraint.IsEqual;8import org.jmock.core.constraint.IsInstanceOf;9import org.jmock.core.constraint.IsSame;10import org.jmock.core.matcher.InvokeOnceMatcher;11import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;12import org.jmock.core.matcher.InvokeAtMostOnceMatcher;13import org.jmock.core.matcher.InvokeAtLeastCountMatcher;14import org.jmock.core.matcher.InvokeAtMostCountMatcher;15import org.jmock.core.matcher.InvokeCountMatcher;16import org.jmock.core.matcher.InvokeBetweenCountMatcher;17import org.jmock.core.matcher.InvokeExactlyCountMatcher;18import org.jmock.core.matcher.InvokeIdMatcher;19import org.jmock.core.matcher.InvokeIdSequenceMatcher;20import org.jmock.core.matcher.InvokeIdInSequenceMatcher;21import org.jmock.core.matcher.InvokeIdNotInSequenceMatcher;22import org.jmock.core.matcher.InvokeAtLeastOnceInSequenceMatcher;23import org.jmock.core.matcher.InvokeAtLeastCountInSequenceMatcher;24import org.jmock.core.matcher.InvokeAtMostOnceInSequenceMatcher;25import org.jmock.core.matcher.InvokeAtMostCountInSequenceMatcher;26import org.jmock.core.matcher.InvokeBetweenCountInSequenceMatcher;27import org.jmock.core.matcher.InvokeExactlyCountInSequenceMatcher;28import org.jmock.core.matcher.InvokeIdInSequenceMatcher;29import org.jmock.core.matcher.InvokeIdNotInSequenceMatcher;30import org.jmock.core.matcher.InvokeAtLeastOnceInSequenceMatcher;31import org.jmock.core.matcher.InvokeAtLeastCountInSequenceMatcher;32import org.jmock.core.matcher.InvokeAtMostOnceInSequenceMatcher;33import org.jmock.core.matcher.InvokeAtMostCountInSequenceMatcher;34import org.jmock.core.matcher.InvokeBetweenCountInSequenceMatcher;35import org.jmock.core.matcher.InvokeExactlyCountInSequenceMatcher;36import org.jmock.core.matcher.InvokeIdInSequenceMatcher;37import org.jmock.core.matcher.InvokeIdNotInSequenceMatcher;38import org.jmock.core.matcher.InvokeAtLeastOnceInSequenceMatcher;39import org.jmock.core.matcher.InvokeAtLeastCountIn
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!!