How to use setAction method of org.jmock.internal.InvocationExpectation class

Best Jmock-library code snippet using org.jmock.internal.InvocationExpectation.setAction

Source:InvocationExpectationTests.java Github

copy

Full Screen

...118 action.expectInvoke = true;119 action.expectedInvocation = invocation;120 action.result = "result";121 122 expectation.setAction(action);123 124 Object actualResult = expectation.invoke(invocation);125 126 assertSame("actual result", action.result, actualResult);127 assertTrue("action1 was invoked", action.wasInvoked);128 }129 130 public void testPerformsSideEffectsWhenInvoked() throws Throwable {131 FakeSideEffect sideEffect1 = new FakeSideEffect();132 FakeSideEffect sideEffect2 = new FakeSideEffect();133 134 expectation.addSideEffect(sideEffect1);135 expectation.addSideEffect(sideEffect2);136 137 Invocation invocation = new Invocation("targetObject", methodFactory.newMethod("method"));138 expectation.invoke(invocation);139 140 assertTrue("side effect 1 should have been performed", sideEffect1.wasPerformed);141 assertTrue("side effect 2 should have been performed", sideEffect2.wasPerformed);142 }143 144 public void testDescriptionIncludesSideEffects() {145 FakeSideEffect sideEffect1 = new FakeSideEffect();146 FakeSideEffect sideEffect2 = new FakeSideEffect();147 148 sideEffect1.descriptionText = "side-effect-1";149 sideEffect2.descriptionText = "side-effect-2";150 151 expectation.addSideEffect(sideEffect1);152 expectation.addSideEffect(sideEffect2);153 154 String description = StringDescription.toString(expectation);155 156 AssertThat.stringIncludes("should include description of sideEffect1",157 sideEffect1.descriptionText, description);158 AssertThat.stringIncludes("should include description of sideEffect2",159 sideEffect2.descriptionText, description);160 161 }162 163 public void testReturnsNullIfHasNoActionsWhenInvoked() throws Throwable {164 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);165 166 Object actualResult = expectation.invoke(invocation);167 168 assertNull("should have returned null", actualResult);169 }170 171 public void testFailsIfActionReturnsAnIncompatibleValue() throws Throwable {172 final Method stringReturningMethod = methodFactory.newMethod("tester", new Class[0], String.class, new Class[0]);173 Invocation invocation = new Invocation(targetObject, stringReturningMethod, Invocation.NO_PARAMETERS);174 ReturnValueAction action = new ReturnValueAction(new Integer(666));175 expectation.setAction(action);176 177 try {178 expectation.invoke(invocation);179 fail("Should have thrown an IllegalStateException");180 } catch (IllegalStateException expected) {181 AssertThat.stringIncludes("Shows returned type", "java.lang.Integer", expected.getMessage());182 AssertThat.stringIncludes("Shows expected return type", "java.lang.String", expected.getMessage());183 }184 }185 186 /**187 * @see CardinalityTests.testHasARequiredAndMaximumNumberOfExpectedInvocations188 */189 public void testHasARequiredAndMaximumNumberOfExpectedInvocations() throws Throwable {...

Full Screen

Full Screen

Source:InvocationExpectationBuilder.java Github

copy

Full Screen

...47 public void addInSequenceOrderingConstraint(Sequence sequence) {48 sequence.constrainAsNextInSequence(expectation);49 }50 51 public void setAction(Action action) {52 expectation.setAction(action);53 needsDefaultAction = false;54 }55 56 public void addSideEffect(SideEffect sideEffect) {57 expectation.addSideEffect(sideEffect);58 }59 60 private <T> T captureExpectedObject(T mockObject) {61 if (!(mockObject instanceof CaptureControl)) {62 throw new IllegalArgumentException("can only set expectations on mock objects");63 }64 65 expectation.setObjectMatcher(new MockObjectMatcher(mockObject));66 isFullySpecified = true;...

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import junit.framework.TestCase;3import org.jmock.Mock;4import org.jmock.MockObjectTestCase;5import org.jmock.core.InvocationExpectation;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.Stub;8import org.jmock.core.constraint.IsAnything;9import org.jmock.core.constraint.IsEqual;10import org.jmock.core.constraint.IsSame;11import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;12import org.jmock.core.matcher.InvokeAtMostOnceMatcher;13import org.jmock.core.matcher.InvokeCountMatcher;14import org.jmock.core.matcher.InvokeOnceMatcher;15import org.jmock.core.matcher.InvokeRangeMatcher;16import org.jmock.core.stub.ReturnStub;17import org.jmock.core.stub.ThrowStub;18{19 public void testSetActionWithStub()20 {21 Mock mock = mock(MockedInterface.class, "mock");22 InvocationExpectation expectation = new InvocationExpectation("mock", null, null, null);23 expectation.setAction(new ReturnStub("result"));24 assertEquals("result", expectation.invoke(null));25 }26 public void testSetActionWithStubAndArgs()27 {28 Mock mock = mock(MockedInterface.class, "mock");29 InvocationExpectation expectation = new InvocationExpectation("mock", null, null, null);30 expectation.setAction(new ReturnStub("result"), new Object[] { "arg1", "arg2" });31 assertEquals("result", expectation.invoke(null));32 }33 public void testSetActionWithStubAndArgsAndMatcher()34 {35 Mock mock = mock(MockedInterface.class, "mock");36 InvocationExpectation expectation = new InvocationExpectation("mock", null, null, null);37 expectation.setAction(new ReturnStub("result"), new Object[] { "arg1", "arg2" }, new IsAnything());38 assertEquals("result", expectation.invoke(null));39 }40 public void testSetActionWithStubAndMatcher()41 {42 Mock mock = mock(MockedInterface.class, "mock");43 InvocationExpectation expectation = new InvocationExpectation("mock", null, null, null);44 expectation.setAction(new ReturnStub("result"), new IsAnything());45 assertEquals("result", expectation.invoke(null));46 }47 public void testSetActionWithStubAndArgsAndMatchers()48 {49 Mock mock = mock(MockedInterface.class, "mock");50 InvocationExpectation expectation = new InvocationExpectation("

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1package test;2import org.jmock.MockObjectTestCase;3import org.jmock.core.InvocationExpectation;4import org.jmock.core.InvocationMatcher;5import org.jmock.core.Stub;6import org.jmock.core.constraint.IsEqual;7import org.jmock.core.constraint.IsAnything;8import org.jmock.core.constraint.IsNot;9import org.jmock.core.constraint.IsSame;10import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;11import org.jmock.core.matcher.InvokeAtMostOnceMatcher;12import org.jmock.core.matcher.InvokeCountMatcher;13import org.jmock.core.matcher.InvokeOnceMatcher;14import org.jmock.core.matcher.InvokeRangeMatcher;15import org.jmock.core.matcher.InvokeRepeatedlyMatcher;16import org.jmock.core.matcher.TestFailureMatcher;17import org.jmock.core.matcher.TestFailureOnceMatcher;18import org.jmock.core.stub.DoAllStub;19import org.jmock.core.stub.DoNothingStub;20import org.jmock.core.stub.DoReturnStub;21import org.jmock.core.stub.DoThrowStub;22import org.jmock.core.stub.InvokeActionStub;23import org.jmock.core.stub.InvokeOnceActionStub;24import org.jmock.core.stub.InvokeRepeatedlyActionStub;25import org.jmock.core.stub.ReturnStub;26import org.jmock.core.stub.ThrowStub;27import org.jmock.core.stub.ThrowThrowableStub;28import org.jmock.util.Action;29import org.jmock.util.Actions;30import org.jmock.util.Dummy;31import org.jmock.util.DummyAction;32import org.jmock.util.DummyThrowable;33public class TestSetAction extends MockObjectTestCase {34 public void testSetAction() {35 InvocationExpectation expectation = new InvocationExpectation("INVOCATION");36 Action action = new DummyAction();37 expectation.setAction(action);38 }39 public void testSetActionNull() {40 InvocationExpectation expectation = new InvocationExpectation("INVOCATION");41 try {42 expectation.setAction(null);43 fail("Should have thrown IllegalArgumentException");44 }45 catch (IllegalArgumentException ex) {46 }47 }48}49package org.jmock.core;50import org.jmock.util.Action;51public class InvocationExpectation extends AbstractExpectation {52 public InvocationExpectation(String description) {53 super(description);54 }55 public void setAction(Action action) {56 if (action == null) {57 throw new IllegalArgumentException("action must not be null");58 }59 this.action = action;60 }61}

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1package org.jmock.internal;2import org.jmock.core.InvocationExpectation;3import org.jmock.core.InvocationExpectationSet;4import org.jmock.core.InvocationMatcher;5import org.jmock.core.Invocation;6import org.jmock.core.InvocationDispatcher;7import org.jmock.core.DynamicMockError;8import org.jmock.core.DynamicMock;9import org.jmock.core.Stub;10import org.jmock.core.Verifiable;11import org.jmock.core.VerifiableList;12import org.jmock.core.constraint.Constraint;13import org.jmock.core.constraint.IsEqual;14import org.jmock.core.constraint.IsAnything;15import org.jmock.core.constraint.IsSame;16import org.jmock.core.constraint.IsInstanceOf;17import org.jmock.core.constraint.IsTypeCompatible;18import org.jmock.core.constraint.IsArrayContaining;19import org.jmock.core.constraint.IsCollectionContaining;20import org.jmock.core.constraint.IsMapContaining;21import org.jmock.core.constraint.IsStringContaining;22import org.jmock.core.constraint.StringEndsWith;23import org.jmock.core.constraint.StringStartsWith;24import org.jmock.core.constraint.StringMatches;25import org.jmock.core.constraint.IsIn;26import org.jmock.core.constraint.IsNot;27import org.jmock.core.constraint.IsNotSame;28import org.jmock.core.constraint.IsLessThan;29import org.jmock.core.constraint.IsGreaterThan;30import org.jmock.core.constraint.IsLessThanOrEqualTo;31import org.jmock.core.constraint.IsGreaterThanOrEqualTo;32import org.jmock.core.constraint.IsBetween;33import org.jmock.core.constraint.IsCloseTo;34import org.jmock.core.constraint.IsNaN;35import org.jmock.core.constraint.IsNull;36import org.jmock.core.constraint.IsNotNull;37import org.jmock.core.constraint.IsSameInstance;38import org.jmock.core.constraint.IsInstanceOfAny;39import org.jmock.core.constraint.IsTypeCompatibleWith;40import org.jmock.core.constraint.IsArrayContainingInOrder;41import org.jmock.core.constraint.IsCollectionContainingInOrder;42import org.jmock.core.constraint.IsMapContainingKey;43import org.jmock.core.constraint.IsMapContainingValue;44import org.jmock.core.constraint.AllOf;45import org.jmock.core.constraint.AnyOf;46import org.jmock.core.constraint.IdentityConstraint;47import org.jmock.core.constraint.IdentityConstraintMatcher;48import org.jmock.core.constraint.Is;49import org.jmock.core.constraint.IsEqualIgnoringCase;50import org.jmock.core.constraint.IsEqualTrimmed;51import org.jmock.core.constraint.IsEqualCompressingWhiteSpace;52import org.jmock.core.constraint.IsEqualNormalizingWhiteSpace

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.Expectations;4{5 public void test()6 {7 final Mock mock = mock(Runnable.class);8 mock.expects(once()).method("run").will(setAction(new Runnable()9 {10 public void run()11 {12 System.out.println("Running...");13 }14 }));15 ((Runnable)mock.proxy()).run();16 }17}

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.InvocationExpectation;5import org.jmock.core.Stub;6import org.jmock.core.StubSequence;7public class 1 extends MockObjectTestCase {8 public void testSetAction() {9 Mock mock = mock(Interface.class);10 mock.expects(once()).method("method1").will(returnValue("one"));11 mock.expects(once()).method("method2").will(returnValue("two"));12 mock.expects(once()).method("method3").will(returnValue("three"));13 Interface i = (Interface) mock.proxy();14 assertEquals("one", i.method1());15 assertEquals("two", i.method2());16 assertEquals("three", i.method3());17 }18}19import org.jmock.Mock;20import org.jmock.MockObjectTestCase;21import org.jmock.core.Invocation;22import org.jmock.core.InvocationExpectation;23import org.jmock.core.Stub;24import org.jmock.core.StubSequence;25public class 2 extends MockObjectTestCase {26 public void testSetAction() {27 Mock mock = mock(Interface.class);28 mock.expects(once()).method("method1").will(returnValue("one"));29 mock.expects(once()).method("method2").will(returnValue("two"));30 mock.expects(once()).method("method3").will(returnValue("three"));31 Interface i = (Interface) mock.proxy();32 assertEquals("one", i.method1());33 assertEquals("two", i.method2());34 assertEquals("three", i.method3());35 }36}37package org.jmock.core;38import java.util.ArrayList;39import java.util.List;40import org.jmock.core.constraint.Constraint;41import org.jmock.core.constraint.IsAnything;42import org.jmock.core.constraint.IsEqual;43import org.jmock.core.constraint.IsSame;44import org.jmock.core.constraint.StringContains;45import org.jmock.core.constraint.StringEndsWith;46import org.jmock.core.constraint.StringStartsWith;

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.Expectations;4import org.jmock.core.Invocation;5import org.jmock.core.InvocationExpectation;6import org.jmock.core.InvocationDispatcher;7import org.jmock.core.InvocationMatcher;8import org.jmock.core.Stub;9import org.jmock.core.Constraint;10import org.jmock.core.DynamicMockError;11import org.jmock.core.InvocationExpectationSet;12import org.jmock.core.InvocationExpectationComparator;13import org.jmock.core.InvocationExpectationComparatorFactory;14import org.jmock.core.StubConstraint;15import org.jmock.core.StubFactory;16import org.jmock.core.StubFactoryList;17import org.jmock.core.StubFactoryListFactory;18import org.jmock.core.StubFactoryListFactoryRegistry;19import org.jmock.core.StubFactoryRegistry;20import org.jmock.core.matcher.InvokeOnceMatcher;21import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;22import org.jmock.core.matcher.InvokeAtMostOnceMatcher;23import org.jmock.core.matcher.InvokeCountMatcher;24import org.jmock.core.matcher.InvokeBetweenMatcher;25import org.jmock.core.matcher.InvokeAtLeastCountMatcher;26import org.jmock.core.matcher.InvokeAtMostCountMatcher;27import org.jmock.core.matcher.InvokeNeverMatcher;28import org.jmock.core.matcher.InvokeIdMatcher;29import org.jmock.core.matcher.InvokeIdSequenceMatcher;30import org.jmock.core.matcher.InvokeIdSetMatcher;31import org.jmock.core.matcher.InvokeIdInOrderMatcher;32import org.jmock.core.matcher.InvokeIdAnyOrderMatcher;33import org.jmock.core.matcher.InvokeIdUnorderedMatcher;34import org.jmock.core.matcher.InvokeIdOrderedMatcher;35import org.jmock.core.matcher.InvokeIdUnorderedSequenceMatcher;36import org.jmock.core.matcher.InvokeIdOrderedSequenceMatcher;37import org.jmock.core.matcher.InvokeIdUnorderedSetMatcher;38import org.jmock.core.matcher.InvokeIdOrderedSetMatcher;39import org.jmock.core.matcher.InvokeIdUnorderedSequenceSetMatcher;40import org.jmock.core.matcher.InvokeIdOrderedSequenceSetMatcher;41import org.jmock.core.matcher.InvokeAtLeastCountInOrderMatcher;42import org.jmock.core.matcher.InvokeAtLeastCountUnorderedMatcher;43import org.jmock.core.matcher.InvokeAtLeastCountOrderedMatcher;44import org.jmock.core.matcher.InvokeAtLeastCountUnorderedSequenceMatcher

Full Screen

Full Screen

setAction

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.InvocationDispatcher;5import org.jmock.core.InvocationExpectation;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.Stub;8import org.jmock.core.constraint.IsEqual;9import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;10import org.jmock.core.matcher.InvokeAtMostOnceMatcher;11import org.jmock.core.matcher.InvokeCountMatcher;12import org.jmock.core.matcher.InvokeOnceMatcher;13import org.jmock.core.matcher.InvokeRecorder;14import org.jmock.core.matcher.InvokeSequenceMatcher;15import org.jmock.core.matcher.InvokeTimesMatcher;16import org.jmock.core.matcher.TestFailureMatcher;17import org.jmock.core.stub.DoAllStub;18import org.jmock.core.stub.DoNothingStub;19import org.jmock.core.stub.DoReturnStub;20import org.jmock.core.stub.DoThrowStub;21import org.jmock.core.stub.InvokeActionStub;22import org.jmock.core.stub.InvokeMatcherStub;23import org.jmock.core.stub.InvokeOnceStub;24import org.jmock.core.stub.InvokeRecorderStub;25import org.jmock.core.stub.InvokeSequenceStub;26import org.jmock.core.stub.InvokeStub;27import org.jmock.core.stub.ThrowStub;28import org.jmock.core.stub.VoidStub;29import org.jmock.util.AssertThat;30import org.jmock.util.Dummy;31public class InvocationDispatcherTest extends MockObjectTestCase {32 private InvocationDispatcher dispatcher;33 private InvocationMatcher matcher;34 private Stub stub;35 private Invocation invocation;36 public void setUp() {37 dispatcher = new InvocationDispatcher();38 matcher = new InvokeOnceMatcher();39 stub = new InvokeOnceStub();40 invocation = new Invocation("INVOKED-OBJECT", "INVOKED-METHOD", new Object[0], new Class[0]);41 }42 public void testCanAddExpectation() {43 dispatcher.addExpectation(new InvocationExpectation(matcher, stub));44 AssertThat.invocationIsMatchedByExpectation(invocation, dispatcher);45 }46 public void testCanAddMultipleExpectations() {47 dispatcher.addExpectation(new InvocationExpectation(matcher, stub));48 dispatcher.addExpectation(new InvocationExpectation(matcher, stub));49 AssertThat.invocationIsMatchedByExpectation(invocation, dispatcher

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful