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

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

Source:InvocationExpectationTests.java Github

copy

Full Screen

...79 Invocation invocation = new Invocation("targetObject", methodFactory.newMethod("method"), Invocation.NO_PARAMETERS);80 81 int maxInvocationCount = 3;82 83 expectation.setCardinality(new Cardinality(0, maxInvocationCount));84 85 int i;86 for (i = 0; i < maxInvocationCount; i++) {87 assertTrue("should match after " + i +" invocations", expectation.matches(invocation));88 expectation.invoke(invocation);89 }90 assertFalse("should not match after " + i + " invocations", expectation.matches(invocation));91 }92 93 public void testMustMeetTheRequiredInvocationCountButContinuesToMatch() throws Throwable {94 Invocation invocation = new Invocation("targetObject", methodFactory.newMethod("method"));95 96 int requiredInvocationCount = 3;97 expectation.setCardinality(new Cardinality(requiredInvocationCount, Integer.MAX_VALUE));98 99 int i;100 for (i = 0; i < requiredInvocationCount; i++) {101 assertTrue("should match after " + i +" invocations", 102 expectation.matches(invocation));103 assertFalse("should not be satisfied after " + i +" invocations",104 expectation.isSatisfied());105 106 expectation.invoke(invocation);107 }108 assertTrue("should match after " + i +" invocations", 109 expectation.matches(invocation));110 assertTrue("should be satisfied after " + i +" invocations",111 expectation.isSatisfied());112 }113 public void testPerformsActionWhenInvoked() throws Throwable {114 final Method stringReturningMethod = methodFactory.newMethod("tester", new Class[0], String.class, new Class[0]);115 Invocation invocation = new Invocation(targetObject, stringReturningMethod, Invocation.NO_PARAMETERS);116 MockAction action = new MockAction();117 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 {190 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);191 192 expectation.setCardinality(new Cardinality(1, 1));193 194 assertTrue(expectation.allowsMoreInvocations());195 assertFalse(expectation.isSatisfied());196 197 expectation.invoke(invocation);198 expectation.invoke(invocation);199 200 assertFalse(expectation.allowsMoreInvocations());201 assertTrue(expectation.isSatisfied());202 }203 204 public void testMatchesIfAllOrderingConstraintsMatch() {205 FakeOrderingConstraint orderingConstraint1 = new FakeOrderingConstraint();206 FakeOrderingConstraint orderingConstraint2 = new FakeOrderingConstraint();207 208 expectation.addOrderingConstraint(orderingConstraint1);209 expectation.addOrderingConstraint(orderingConstraint2);210 211 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);212 213 orderingConstraint1.allowsInvocationNow = true;214 orderingConstraint2.allowsInvocationNow = true;215 assertTrue(expectation.matches(invocation));216 217 orderingConstraint1.allowsInvocationNow = true;218 orderingConstraint2.allowsInvocationNow = false;219 assertFalse(expectation.matches(invocation));220 221 orderingConstraint1.allowsInvocationNow = false;222 orderingConstraint2.allowsInvocationNow = true;223 assertFalse(expectation.matches(invocation));224 225 orderingConstraint1.allowsInvocationNow = false;226 orderingConstraint2.allowsInvocationNow = false;227 assertFalse(expectation.matches(invocation));228 }229 230 public void testDescriptionIncludesCardinality() {231 final Cardinality cardinality = new Cardinality(2, 2);232 expectation.setCardinality(cardinality);233 234 AssertThat.stringIncludes("should include cardinality description",235 StringDescription.toString(cardinality), 236 StringDescription.toString(expectation));237 }238 239 public void testDescribesNumberOfInvocationsReceived() throws Throwable {240 Invocation invocation = new Invocation(targetObject, method, Invocation.NO_PARAMETERS);241 242 expectation.setCardinality(new Cardinality(2,3));243 244 AssertThat.stringIncludes("should describe as not invoked",245 "never invoked", StringDescription.toString(expectation));246 247 expectation.invoke(invocation);248 AssertThat.stringIncludes("should describe as not invoked",249 "invoked 1 time", StringDescription.toString(expectation));250 251 expectation.invoke(invocation);252 expectation.invoke(invocation);253 AssertThat.stringIncludes("should describe as not invoked",254 "invoked 3 times", StringDescription.toString(expectation));255 }256 public static class FakeOrderingConstraint implements OrderingConstraint {...

Full Screen

Full Screen

Source:InvocationExpectationBuilder.java Github

copy

Full Screen

...31 32 return expectation;33 }34 35 public void setCardinality(Cardinality cardinality) {36 expectation.setCardinality(cardinality);37 }38 39 public void addParameterMatcher(Matcher<?> matcher) {40 capturedParameterMatchers.add(matcher);41 }42 43 public void addOrderingConstraint(OrderingConstraint constraint) {44 expectation.addOrderingConstraint(constraint);45 }46 47 public void addInSequenceOrderingConstraint(Sequence sequence) {48 sequence.constrainAsNextInSequence(expectation);49 }50 ...

Full Screen

Full Screen

setCardinality

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.InvocationExpectationSet;7import org.jmock.core.Stub;8{9 public void testSetCardinalityOfInvocationExpectation() throws Exception10 {

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.InvocationExpectation;4import org.jmock.core.InvocationExpectationSet;5import org.jmock.core.InvocationMatcher;6import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;7import org.jmock.core.matcher.InvokeAtMostOnceMatcher;8import org.jmock.core.matcher.InvokeCountMatcher;9import org.jmock.core.matcher.InvokeOnceMatcher;10import org.jmock.core.matcher.InvokeRangeMatcher;11{12 public void testSetCardinality()13 {14 Mock mock = mock(InvocationExpectationSet.class);15 InvocationExpectationSet set = (InvocationExpectationSet) mock.proxy();16 InvocationExpectation expectation = new InvocationExpectation(new InvocationMatcher[]{new InvokeOnceMatcher()});17 set.add(expectation);18 set.setCardinality(expectation, new InvokeAtLeastOnceMatcher());19 assertEquals("1", 1, set.size());20 assertEquals("2", new InvokeAtLeastOnceMatcher(), ((InvocationExpectation) set.iterator().next()).getInvocationMatcher());21 set.setCardinality(expectation, new InvokeAtMostOnceMatcher());22 assertEquals("3", 1, set.size());23 assertEquals("4", new InvokeAtMostOnceMatcher(), ((InvocationExpectation) set.iterator().next()).getInvocationMatcher());24 set.setCardinality(expectation, new InvokeCountMatcher(3));25 assertEquals("5", 1, set.size());26 assertEquals("6", new InvokeCountMatcher(3), ((InvocationExpectation) set.iterator().next()).getInvocationMatcher());27 set.setCardinality(expectation, new InvokeRangeMatcher(1, 5));28 assertEquals("7", 1, set.size());29 assertEquals("8", new InvokeRangeMatcher(1, 5), ((InvocationExpectation) set.iterator().next()).getInvocationMatcher());30 }31}

Full Screen

Full Screen

setCardinality

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.Constraint;6import org.jmock.core.InvocationExpectation;7import org.jmock.core.constraint.IsEqual;8public class MockObjectTestCaseAcceptanceTest_1 extends TestCase {9 public void testSetCardinalityMethod() {10 Mock mock = new MockObjectTestCase().mock(Interface.class);11 Constraint constraint = new IsEqual(new Integer(1));12 InvocationExpectation invocationExpectation = new InvocationExpectation("method", new Object[]{constraint}, 1);13 mock.expects(invocationExpectation);14 mock.setCardinality(2);15 Interface i = (Interface) mock.proxy();16 i.method(1);17 i.method(1);18 i.method(1);19 i.method(1);20 }21 public static interface Interface {22 void method(Integer i);23 }24}

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.InvocationExpectation;4import org.jmock.core.Stub;5import org.jmock.core.Invocation;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.Constraint;8{9 public void testSetCardinality()10 {11 Mock mock = mock(Stub.class);12 InvocationExpectation expectation = new InvocationExpectation("method", new InvocationMatcher() {13 public boolean matches(Invocation invocation) {14 return true;15 }16 public StringBuffer describeTo(StringBuffer buffer) {17 return buffer;18 }19 });20 expectation.setCardinality(5);21 mock.expects(expectation);22 mock.stubs().method("method").will(returnValue("value"));23 for (int i = 0; i < 5; i++) {24 assertEquals("value", ((Stub)mock.proxy()).method());25 }26 }27}28import org.jmock.Mock;29import org.jmock.MockObjectTestCase;30import org.jmock.core.InvocationExpectation;31import org.jmock.core.Stub;32import org.jmock.core.Invocation;33import org.jmock.core.InvocationMatcher;34import org.jmock.core.Constraint;35{36 public void testSetCardinality()37 {38 Mock mock = mock(Stub.class);39 InvocationExpectation expectation = new InvocationExpectation("method", new InvocationMatcher() {40 public boolean matches(Invocation invocation) {41 return true;42 }43 public StringBuffer describeTo(StringBuffer buffer) {44 return buffer;45 }46 });47 expectation.setCardinality(5);48 mock.expects(expectation);49 mock.stubs().method("method").will(returnValue("value"));50 for (int i = 0; i < 5; i++) {51 assertEquals("value", ((Stub)mock.proxy()).method());52 }53 }54}55import org.jmock.Mock;56import org.jmock.MockObjectTestCase;57import org.jmock.core.InvocationExpectation;58import org.jmock.core.Stub;59import org.jmock.core.Invocation;60import org.jmock

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.InvocationExpectation;4import org.jmock.core.InvocationExpectationCounter;5import org.jmock.core.InvocationExpectationCounterFactory;6import org.jmock.core.InvocationExpectationCounterFactoryImpl;7import org.jmock.core.InvocationExpectationCounterImpl;8import org.jmock.core.InvocationExpectationList;9import org.jmock.core.InvocationExpectationListImpl;10import org.jmock.core.InvocationExpectationSet;11import org.jmock.core.InvocationExpectationSetImpl;12import org.jmock.core.InvocationExpectationSetMatcher;13import org.jmock.core.InvocationExpectationSetMatcherImpl;14import org.jmock.core.InvocationExpectationSetMatcherList;15import org.jmock.core.InvocationExpectationSetMatcherListImpl;16import org.jmock.core.InvocationExpectationSetMatcherSet;17import org.jmock.core.InvocationExpectationSetMatcherSetImpl;18import org.jmock.core.InvocationExpectationSetMatcherSetMatcher;19import org.jmock.core.InvocationExpectationSetMatcherSetMatcherImpl;20import org.jmock.core.InvocationExpectationSetMatcherSetMatcherList;21import org.jmock.core.InvocationExpectationSetMatcherSetMatcherListImpl;22import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSet;23import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetImpl;24import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcher;25import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherImpl;26import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherList;27import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherListImpl;28import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSet;29import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSetImpl;30import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSetMatcher;31import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSetMatcherImpl;32import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSetMatcherList;33import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSetMatcherListImpl;34import org.jmock.core.InvocationExpectationSetMatcherSetMatcherSetMatcherSetMatcherSet;35import org.jmock.core.InvocationExpectationSetMatcherSet

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import org.jmock.Mock;3import org.jmock.MockObjectTestCase;4import org.jmock.core.InvocationExpectation;5import org.jmock.core.InvocationExpectationSet;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.InvocationMocker;8import org.jmock.core.InvocationStubber;9import org.jmock.core.InvocationStubberImpl;10import org.jmock.core.Stub;

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1import junit.framework.TestCase;2import org.jmock.core.Invocation;3import org.jmock.core.InvocationExpectation;4import org.jmock.core.InvocationMatcher;5import org.jmock.core.constraint.IsEqual;6import org.jmock.core.constraint.IsAnything;7import org.jmock.core.constraint.IsEqual;8import org.jmock.core.constraint.IsAnything;9import org.jmock.core.constraint.IsSame;10import org.jmock.core.constraint.IsSame;11public class Test1 extends TestCase {12 public void test1() {13 InvocationExpectation expectation = new InvocationExpectation("foo", new IsAnything());14 expectation.setCardinality(1, 1);15 expectation.setCardinality(2, 2);16 expectation.setCardinality(1, 2);17 expectation.setCardinality(0, 1);18 expectation.setCardinality(0, 0);19 expectation.setCardinality(1, 1);20 expectation.setCardinality(2, 2);21 expectation.setCardinality(2, 2);22 expectation.setCardinality(1, 2);23 expectation.setCardinality(0, 1);24 expectation.setCardinality(0, 0);25 expectation.setCardinality(0, 1);26 expectation.setCardinality(0, 0);27 expectation.setCardinality(1, 2);28 expectation.setCardinality(0, 1);29 expectation.setCardinality(0, 0);30 expectation.setCardinality(0, 1);31 expectation.setCardinality(0, 0);32 expectation.setCardinality(1, 2);33 expectation.setCardinality(0, 1);34 expectation.setCardinality(0, 0);35 expectation.setCardinality(0, 1);36 expectation.setCardinality(0, 0);37 expectation.setCardinality(1, 2);38 expectation.setCardinality(0, 1);39 expectation.setCardinality(0, 0);40 expectation.setCardinality(0, 1);41 expectation.setCardinality(0, 0);42 expectation.setCardinality(1, 2);43 expectation.setCardinality(0, 1);44 expectation.setCardinality(0, 0);45 expectation.setCardinality(0, 1);46 expectation.setCardinality(0, 0);47 expectation.setCardinality(1, 2);48 expectation.setCardinality(0

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import junit.framework.TestCase;3import org.jmock.Mock;4import org.jmock.core.InvocationExpectation;5import org.jmock.core.InvocationExpectationSet;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.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.InvokeNeverMatcher;17import org.jmock.core.matcher.InvokeRangeMatcher;18import org.jmock.core.matcher.InvokeAtLeastCountRangeMatcher;19import org.jmock.core.matcher.InvokeAtMostCountRangeMatcher;20import org.jmock.core.matcher.InvokeCountRangeMatcher;21import org.jmock.core.matcher.InvokeAtLeastCountBetweenMatcher;22import org.jmock.core.matcher.InvokeAtMostCountBetweenMatcher;23import org.jmock.core.matcher.InvokeCountBetweenMatcher;24import org.jmock.core.matcher.InvokeAtLeastCountBetweenRangeMatcher;25import org.jmock.core.matcher.InvokeAtMostCountBetweenRangeMatcher;26import org.jmock.core.matcher.InvokeCountBetweenRangeMatcher;27public class InvocationExpectationSetAcceptanceTests extends TestCase {28 Mock mockMockObject = new Mock(MockObject.class);29 MockObject mockObject = (MockObject)mockMockObject.proxy();30 Mock mockStub = new Mock(Stub.class);31 Stub stub = (Stub)mockStub.proxy();32 InvocationExpectationSet expectations = new InvocationExpectationSet();33 public void testCanAddExpectation() {34 expectations.add(new InvocationExpectation("method1", new IsAnything(), new IsEqual("result1")));35 }36 public void testCanAddExpectationWithStub() {37 expectations.add(new InvocationExpectation("method1", new IsAnything(), stub));38 }39 public void testCanAddExpectationWithStubAndCardinality() {40 expectations.add(new InvocationExpectation("method1", new IsAnything(), stub, new InvokeOnceMatcher()));41 }42 public void testCanAddExpectationWithCardinality() {43 expectations.add(new InvocationExpectation("method1", new IsAnything(), new IsEqual("result1"), new InvokeOnceMatcher()));44 }

Full Screen

Full Screen

setCardinality

Using AI Code Generation

copy

Full Screen

1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.InvocationExpectation;4import org.jmock.core.Invocation;5import org.jmock.core.Constraint;6public class SetCardinality extends MockObjectTestCase{7 public void testSetCardinality(){8 Mock mock = mock(Constraint.class);9 Invocation invocation = new Invocation("foo", new Object[]{}, new Class[]{});10 InvocationExpectation expectation = new InvocationExpectation("foo", invocation, (Constraint) mock.proxy());11 expectation.setCardinality(1, 1);12 assertEquals(1, expectation.getMinimumInvocations());13 assertEquals(1, expectation.getMaximumInvocations());14 }15}16import org.jmock.MockObjectTestCase;17import org.jmock.Mock;18import org.jmock.core.InvocationExpectation;19import org.jmock.core.Invocation;20import org.jmock.core.Constraint;21public class SetCardinality extends MockObjectTestCase{22 public void testSetCardinality(){23 Mock mock = mock(Constraint.class);24 Invocation invocation = new Invocation("foo", new Object[]{}, new Class[]{});25 InvocationExpectation expectation = new InvocationExpectation("foo", invocation, (Constraint) mock.proxy());26 expectation.setCardinality(1, 1);27 assertEquals(1, expectation.getMinimumInvocations());28 assertEquals(1, expectation.getMaximumInvocations());29 }30}31import org.jmock.MockObjectTestCase;32import org.jmock.Mock;33import org.jmock.core.InvocationExpectation;34import org.jmock.core.Invocation;35import org.jmock.core.Constraint;36public class SetCardinality extends MockObjectTestCase{37 public void testSetCardinality(){38 Mock mock = mock(Constraint.class);39 Invocation invocation = new Invocation("foo", new Object[]{}, new Class[]{});40 InvocationExpectation expectation = new InvocationExpectation("foo", invocation, (Constraint) mock.proxy());41 expectation.setCardinality(1, 1);42 assertEquals(1, expectation.getMinimumInvocations());43 assertEquals(1, expectation.getMaximumInvocations());44 }45}

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