Best Jmock-library code snippet using org.jmock.lib.action.ActionSequence.ActionSequence
Source:ActionSequenceTests.java
...6import org.hamcrest.StringDescription;7import org.jmock.api.Action;8import org.jmock.api.ExpectationError;9import org.jmock.api.Invocation;10import org.jmock.lib.action.ActionSequence;11import org.jmock.test.unit.support.AssertThat;12import org.jmock.test.unit.support.MethodFactory;13import org.jmock.test.unit.support.MockAction;14public class ActionSequenceTests extends TestCase {15 private Object invokedObject = "INVOKED_OBJECT";16 private MethodFactory methodFactory = new MethodFactory();17 private Method invokedMethod = methodFactory.newMethodReturning(String.class);18 private Invocation invocation = new Invocation(invokedObject, invokedMethod);19 20 21 @SuppressWarnings("cast") // Eclipse gives warning if there is a cast and if there is not!22 public void testInvokesActionsInOrder() throws Throwable {23 final int sequenceLength = 4;24 25 MockAction[] actions = new MockAction[sequenceLength]; 26 for (int i = 0; i < sequenceLength; i++) {27 actions[i] = new MockAction();28 actions[i].result = "RESULT-" + i;29 if (i > 0) actions[i].previous = actions[i-1];30 }31 32 Invocation[] invocations = new Invocation[actions.length];33 for (int i = 0; i < sequenceLength; i++) {34 invocations[i] = new Invocation(invokedObject, invokedMethod);35 }36 37 ActionSequence sequence = new ActionSequence((Action[])actions);38 39 for (int current = 0; current < actions.length; current++) {40 reset(actions);41 actions[current].expectInvoke = true;42 actions[current].expectedInvocation = invocation;43 44 Object result = sequence.invoke(invocation);45 46 assertSame("should be result of actions[" + current + "]",47 actions[current].result, result);48 }49 }50 51 @SuppressWarnings("cast") // Eclipse gives warning if there is a cast and if there is not!52 public void testFailsIfInvokedMoreTimesThanThereAreActionsInTheSequence() throws Throwable {53 MockAction[] actions = new MockAction[]{new MockAction(), new MockAction()};54 ActionSequence sequence = new ActionSequence((Action[])actions);55 56 for (int i = 0; i < actions.length; i++) sequence.invoke(invocation);57 58 try {59 sequence.invoke(invocation);60 fail("should have thrown IllegalStateException");61 }62 catch (ExpectationError ex) {63 AssertThat.stringIncludes("should describe error",64 "no more actions", ex.getMessage());65 return;66 }67 }68 69 @SuppressWarnings("cast") // Eclipse gives warning if there is a cast and if there is not!70 public void testDescribesItselfAsSequenceOfActions() throws Throwable {71 MockAction[] actions = new MockAction[]{new MockAction(), new MockAction()};72 ActionSequence sequence = new ActionSequence((Action[])actions);73 74 String sequenceDescription = StringDescription.toString(sequence);75 for (int i = 0; i < actions.length; i++) {76 AssertThat.stringIncludes("should include action " + i,77 actions[i].descriptionText, sequenceDescription);78 if (i > 0) {79 int h = i - 1;80 81 assertTrue("description of action " + h + " should be before that of action " + i,82 sequenceDescription.indexOf(actions[h].descriptionText) <83 sequenceDescription.indexOf(actions[i].descriptionText));84 }85 }86 }...
Source:ActionSequence.java
...14 * 15 * @author nat16 *17 */18public class ActionSequence implements Action {19 List<Action> actions;20 Iterator<Action> iterator;21 22 public ActionSequence(Action... actions) {23 this.actions = new ArrayList<Action>(Arrays.asList(actions));24 this.iterator = this.actions.iterator();25 }26 27 public Object invoke(Invocation invocation) throws Throwable {28 if (iterator.hasNext()) {29 return iterator.next().invoke(invocation);30 } 31 throw ExpectationError.unexpected("no more actions available", invocation);32 }33 34 public void describeTo(Description description) {35 description.appendList("", ", and then ", "", actions);36 }...
ActionSequence
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.constraint.IsAnything;7import org.jmock.core.constraint.IsInstanceOf;8import org.jmock.core.constraint.IsSame;9import org.jmock.core.matcher.InvokeOnceMatcher;10import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;11import org.jmock.core.matcher.InvokeAtMostOnceMatcher;12import org.jmock.core.matcher.InvokeAtLeastCountMatcher;13import org.jmock.core.matcher.InvokeAtMostCountMatcher;14import org.jmock.core.matcher.InvokeCountMatcher;15import org.jmock.core.matcher.InvokeBetweenCountMatcher;16import org.jmock.core.matcher.InvokeNeverMatcher;17import org.jmock.core.matcher.InvokeRangeMatcher;18import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;19import org.jmock.core.matcher.InvokeAtMostOnceMatcher;20import org.jmock.core.matcher.InvokeAtLeastCountMatcher;21import org.jmock.core.matcher.InvokeAtMostCountMatcher;22import org.jmock.core.matcher.InvokeCountMatcher;23import org.jmock.core.matcher.InvokeBetweenCountMatcher;24import org.jmock.core.matcher.InvokeNeverMatcher;25import org.jmock.core.matcher.InvokeRangeMatcher;26import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;27import org.jmock.core.matcher.InvokeAtMostOnceMatcher;28import org.jmock.core.matcher.InvokeAtLeastCountMatcher;29import org.jmock.core.matcher.InvokeAtMostCountMatcher;30import org.jmock.core.matcher.InvokeCountMatcher;31import org.jmock.core.matcher.InvokeBetweenCountMatcher;32import org.jmock.core.matcher.InvokeNeverMatcher;33import org.jmock.core.matcher.InvokeRangeMatcher;34import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;35import org.jmock.core.matcher.InvokeAtMostOnceMatcher;36import org.jmock.core.matcher.InvokeAtLeastCountMatcher;37import org.jmock.core.matcher.InvokeAtMostCountMatcher;38import org.jmock.core.matcher.InvokeCountMatcher;39import org.jmock.core.matcher.InvokeBetweenCountMatcher;40import org.jmock.core.matcher.InvokeNeverMatcher;41import org.jmock.core.matcher.InvokeRangeMatcher;42import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;43import org.jmock.core.matcher.InvokeAtMostOnceMatcher;44import org.jmock.core.matcher.InvokeAtLeastCountMatcher;45import org.jmock.core.matcher.InvokeAtMostCountMatcher;46import org.jmock.core.matcher.InvokeCountMatcher;47import org.jmock.core.matcher.InvokeBetween
ActionSequence
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Constraint;4import org.jmock.core.constraint.IsEqual;5import org.jmock.core.constraint.IsAnything;6import org.jmock.core.constraint.IsInstanceOf;7import org.jmock.core.constraint.IsSame;8import org.jmock.core.constraint.IsNot;9import org.jmock.core.constraint.IsNotSame;10import org.jmock.core.constraint.IsCollectionContaining;11import org.jmock.core.constraint.IsNull;12import org.jmock.core.constraint.IsNotNull;13import org.jmock.core.constraint.IsLessThan;14import org.jmock.core.constraint.IsGreaterThan;15import org.jmock.core.constraint.IsIn;16import org.jmock.core.constraint.IsNotIn;17import org.jmock.core.constraint.IsInstanceOf;18import org.jmock.core.constraint.IsTypeCompatibleWith;19import org.jmock.core.constraint.IsStringStarting;20import org.jmock.core.constraint.IsStringEnding;21import org.jmock.core.constraint.IsStringContaining;22import org.jmock.core.constraint.IsStringMatching;23import org.jmock.core.constraint.Is;24import org.jmock.core.constraint.IsEqual;25import org.jmock.core.constraint.IsNotEqual;26import org.jmock.core.constraint.IsSame;27import org.jmock.core.constraint.IsNotSame;28import org.jmock.core.constraint.IsIdentical;29import org.jmock.core.constraint.IsNotIdentical;30import org.jmock.core.constraint.IsInstanceOf;31import org.jmock.core.constraint.IsNotInstanceOf;32import org.jmock.core.constraint.IsTypeCompatibleWith;33import org.jmock.core.constraint.IsNotTypeCompatibleWith;34import org.jmock.core.constraint.IsCollectionContaining;35import org.jmock.core.constraint.IsCollectionNotContaining;36import org.jmock.core.constraint.IsArrayContaining;37import org.jmock.core.constraint.IsArrayNotContaining;38import org.jmock.core.constraint.IsMapContaining;39import org.jmock.core.constraint.IsMapNotContaining;40import org.jmock.core.constraint.Is;41import org.jmock.core.constraint.IsEqual;42import org.jmock.core.constraint.IsNotEqual;43import org.jmock.core.constraint.IsSame;44import org.jmock.core.constraint.IsNotSame;45import org.jmock.core.constraint.IsIdentical;46import org.jmock.core.constraint.IsNotIdentical;47import org.jmock.core.constraint.IsInstanceOf;48import org.jmock.core.constraint.IsNotInstanceOf;49import org.jmock.core.constraint.IsTypeCompatibleWith;50import org.jmock.core.constraint.IsNotTypeCompatibleWith;51import org.jmock.core.constraint.IsCollectionContaining;52import org.jmock.core
ActionSequence
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.Invocation;4import org.jmock.core.Stub;5import org.jmock.lib.action.ActionSequence;6import org.jmock.lib.action.ReturnValueAction;7public class 1 extends MockObjectTestCase {8 public void testActionSequence() {9 Mock mock = mock(Interface.class);10 mock.stubs().method("doSomething").will(11 new ActionSequence(12 new ReturnValueAction("first"),13 new ReturnValueAction("second"),14 new ReturnValueAction("third")15 );16 Interface proxy = (Interface)mock.proxy();17 assertEquals("first", proxy.doSomething());18 assertEquals("second", proxy.doSomething());19 assertEquals("third", proxy.doSomething());20 }21}22import org.jmock.MockObjectTestCase;23import org.jmock.Mock;24import org.jmock.core.Invocation;25import org.jmock.core.Stub;26import org.jmock.lib.action.ActionSequence;27import org.jmock.lib.action.ReturnValueAction;28public class 2 extends MockObjectTestCase {29 public void testActionSequence() {30 Mock mock = mock(Interface.class);31 mock.stubs().method("doSomething").will(32 new ActionSequence(33 new ReturnValueAction("first"),34 new ReturnValueAction("second"),35 new ReturnValueAction("third")36 );37 Interface proxy = (Interface)mock.proxy();38 assertEquals("first", proxy.doSomething());39 assertEquals("second", proxy.doSomething());40 assertEquals("third", proxy.doSomething());41 }42}43import org.jmock.MockObjectTestCase;44import org.jmock.Mock;45import org.jmock.core.Invocation;46import org.jmock.core.Stub;47import org.jmock.lib.action.ActionSequence;48import org.jmock.lib.action.ReturnValueAction;49public class 3 extends MockObjectTestCase {50 public void testActionSequence() {51 Mock mock = mock(Interface.class);52 mock.stubs().method("doSomething").will(53 new ActionSequence(54 new ReturnValueAction("first"),55 new ReturnValueAction("second"),56 new ReturnValueAction("third")57 );58 Interface proxy = (Interface)mock.proxy();59 assertEquals("first", proxy.doSomething());
ActionSequence
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.Invocation;4import org.jmock.core.stub.DefaultResultStub;5import org.jmock.core.stub.Stub;6import org.jmock.lib.action.ActionSequence;7import org.jmock.lib.action.ReturnValueAction;8import org.jmock.lib.action.ThrowAction;9import org.jmock.lib.action.VoidAction;10public class ActionSequenceTest extends MockObjectTestCase {11 private Mock mockObject;12 private Stub stub;13 public void setUp() {14 mockObject = mock(Interface.class);15 stub = new DefaultResultStub();16 }17 public void testReturnsValuesInSequence() {18 mockObject.stubs().method("doSomething").will(19 new ActionSequence(20 new ReturnValueAction("a"),21 new ReturnValueAction("b"),22 new ReturnValueAction("c")));23 Interface object = (Interface)mockObject.proxy();24 assertEquals("a", object.doSomething());25 assertEquals("b", object.doSomething());26 assertEquals("c", object.doSomething());27 }28 public void testThrowsExceptionsInSequence() {29 mockObject.stubs().method("doSomething").will(30 new ActionSequence(31 new ThrowAction(new RuntimeException("a")),32 new ThrowAction(new RuntimeException("b")),33 new ThrowAction(new RuntimeException("c"))));34 Interface object = (Interface)mockObject.proxy();35 try {36 object.doSomething();37 fail("Should have thrown exception");38 } catch (RuntimeException e) {39 assertEquals("a", e.getMessage());40 }41 try {42 object.doSomething();43 fail("Should have thrown exception");44 } catch (RuntimeException e) {45 assertEquals("b", e.getMessage());46 }47 try {48 object.doSomething();49 fail("Should have thrown exception");50 } catch (RuntimeException e) {51 assertEquals("c", e.getMessage());52 }53 }54 public void testDoesNothingInSequence() {55 mockObject.stubs().method("doSomething").will(56 new ActionSequence(57 new VoidAction(),58 new VoidAction(),59 new VoidAction()));60 Interface object = (Interface)mockObject.proxy();61 object.doSomething();62 object.doSomething();63 object.doSomething();64 }65 public void testThrowsExceptionIfNoActionsLeft() {66 mockObject.stubs().method("doSomething").will(67 new ActionSequence(68 new ReturnValueAction("a"),69 new ReturnValueAction("b
ActionSequence
Using AI Code Generation
1package org.jmock.example;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.core.Stub;6import org.jmock.core.stub.StubSequence;7import org.jmock.core.stub.ReturnStub;8import org.jmock.core.stub.ThrowStub;9import org.jmock.example.Calculator;10import org.jmock.example.CalculatorException;11import org.jmock.example.CalculatorUser;12public class CalculatorUserTest extends MockObjectTestCase {13 Mock mockCalculator = mock(Calculator.class);14 CalculatorUser user = new CalculatorUser((Calculator)mockCalculator.proxy());15 public void testReturnsResultOfAddition() {16 mockCalculator.expects(once()).method("add")17 .with(eq(2), eq(2))18 .will(returnValue(4));19 mockCalculator.expects(once()).method("add")20 .with(eq(4), eq(2))21 .will(returnValue(6));22 mockCalculator.expects(once()).method("add")23 .with(eq(6), eq(2))24 .will(returnValue(8));25 assertEquals(4, user.add(2, 2));26 assertEquals(6, user.add(4, 2));27 assertEquals(8, user.add(6, 2));28 }29 public void testReturnsResultOfAdditionUsingActionSequence() {30 mockCalculator.expects(once()).method("add")31 .with(eq(2), eq(2))32 .will(new ActionSequence(new Object[] {33 returnValue(4),34 returnValue(6),35 returnValue(8)36 }));37 assertEquals(4, user.add(2, 2));38 assertEquals(6, user.add(4, 2));39 assertEquals(8, user.add(6, 2));40 }41 public void testReturnsResultOfAdditionUsingActionSequenceWithStubs() {42 mockCalculator.expects(once()).method("add")43 .with(eq(2), eq(2))44 .will(new ActionSequence(new Object[] {45 returnValue(4),46 returnValue(6),47 returnValue(8)48 }));
ActionSequence
Using AI Code Generation
1package org.jmock.example;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.Invocation;5import org.jmock.core.Stub;6import org.jmock.core.stub.ReturnStub;7import org.jmock.core.stub.ThrowStub;8import org.jmock.core.stub.StubSequence;9import org.jmock.core.stub.StubSequenceBuilder;10import org.jmock.core.stub.StubSequenceBuilder.SequenceBuilder;
ActionSequence
Using AI Code Generation
1import org.jmock.*;2import org.jmock.lib.action.*;3import org.jmock.lib.legacy.ClassImposteriser;4{5 public void testActionSequence()6 {7 Mock mock = mock(Interface.class, "mock");8 Interface i = (Interface)mock.proxy();9 mock.expects(once()).method("doSomething").will(returnValue("1"));10 mock.expects(once()).method("doSomething").will(returnValue("2"));11 mock.expects(once()).method("doSomething").will(returnValue("3"));12 mock.expects(once()).method("doSomething").will(returnValue("4"));13 mock.expects(once()).method("doSomething").will(returnValue("5"));14 ActionSequence actionSequence = new ActionSequence();15 actionSequence.addAction(new ReturnValueAction("1"));16 actionSequence.addAction(new ReturnValueAction("2"));17 actionSequence.addAction(new ReturnValueAction("3"));18 actionSequence.addAction(new ReturnValueAction("4"));19 actionSequence.addAction(new ReturnValueAction("5"));20 mock.expects(once()).method("doSomething").will(actionSequence);21 assertEquals("1", i.doSomething());22 assertEquals("2", i.doSomething());23 assertEquals("3", i.doSomething());24 assertEquals("4", i.doSomething());25 assertEquals("5", i.doSomething());26 }27}28import org.jmock.*;29import org.jmock.lib.action.*;30import org.jmock.lib.legacy.ClassImposteriser;31{32 public void testActionSequence()33 {34 Mock mock = mock(Interface.class, "mock");35 Interface i = (Interface)mock.proxy();36 mock.expects(once()).method("doSomething").will(returnValue("1"));37 mock.expects(once()).method("doSomething").will(returnValue("2"));38 mock.expects(once()).method("doSomething").will(returnValue("3"));39 mock.expects(once()).method("doSomething").will(returnValue("4"));40 mock.expects(once()).method("doSomething").will(returnValue("5"));41 ActionSequence actionSequence = new ActionSequence();42 actionSequence.addAction(new ReturnValueAction("1"));43 actionSequence.addAction(new ReturnValueAction("2"));44 actionSequence.addAction(new ReturnValue
ActionSequence
Using AI Code Generation
1import org.jmock.*;2import org.jmock.lib.action.ActionSequence;3import org.jmock.lib.action.ReturnValueAction;4{5 public static void main(String[] args)6 {7 MockObject mock = new MockObject();8 Mock mockObject = mock.mock();9 ActionSequence sequence = new ActionSequence();10 sequence.addAction(new ReturnValueAction("First Action"));11 sequence.addAction(new ReturnValueAction("Second Action"));12 mockObject.expects(once()).method("method").will(sequence);13 System.out.println(mockObject.proxy().method());14 System.out.println(mockObject.proxy().method());15 }16}17import org.jmock.*;18import org.jmock.lib.action.ActionSequence;19import org.jmock.lib.action.ReturnValueAction;20{21 public static void main(String[] args)22 {23 MockObject mock = new MockObject();24 Mock mockObject = mock.mock();25 ActionSequence sequence = new ActionSequence();26 sequence.addAction(new ReturnValueAction("First Action"));27 sequence.addAction(new ReturnValueAction("Second Action"));28 mockObject.expects(once()).method("method").will(sequence);29 System.out.println(mockObject.proxy().method());30 System.out.println(mockObject.proxy().method());31 }32}33import org.jmock.*;34import org.jmock.lib.action.ActionSequence;35import org.jmock.lib.action.ReturnValueAction;36{37 public static void main(String[] args)38 {39 MockObject mock = new MockObject();
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!!