How to use ParametersMatcher method of org.jmock.internal.matcher.ParametersMatcher class

Best Jmock-library code snippet using org.jmock.internal.matcher.ParametersMatcher.ParametersMatcher

Source:InvocationExpectation.java Github

copy

Full Screen

...17 * @author npryce18 * @author smgf19 */20public class InvocationExpectation implements Expectation {21 private static ParametersMatcher ANY_PARAMETERS = new AnyParametersMatcher();22 private Cardinality cardinality = Cardinality.ALLOWING;23 private Matcher<?> objectMatcher = IsAnything.anything();24 private Matcher<? super Method> methodMatcher = IsAnything.anything("<any method>");25 private boolean methodIsKnownToBeVoid = false;26 private ParametersMatcher parametersMatcher = ANY_PARAMETERS;27 private Action action = new VoidAction();28 private boolean actionIsDefault = true;29 private List<OrderingConstraint> orderingConstraints = new ArrayList<OrderingConstraint>();30 private List<SideEffect> sideEffects = new ArrayList<SideEffect>();31 32 private int invocationCount = 0;33 34 public void setCardinality(Cardinality cardinality) {35 this.cardinality = cardinality;36 }37 38 public void setObjectMatcher(Matcher<?> objectMatcher) {39 this.objectMatcher = objectMatcher;40 }41 42 public void setMethod(Method method) {43 this.methodMatcher = new MethodMatcher(method);44 this.methodIsKnownToBeVoid = method.getReturnType() == void.class;45 }46 47 public void setMethodMatcher(Matcher<? super Method> matcher) {48 this.methodMatcher = matcher;49 this.methodIsKnownToBeVoid = false;50 }51 52 public void setParametersMatcher(ParametersMatcher parametersMatcher) {53 this.parametersMatcher = parametersMatcher;54 }55 public void addOrderingConstraint(OrderingConstraint orderingConstraint) {56 orderingConstraints.add(orderingConstraint);57 }58 public void addSideEffect(SideEffect sideEffect) {59 sideEffects.add(sideEffect);60 }61 62 public void setAction(Action action) {63 this.action = action;64 this.actionIsDefault = false;65 }66 67 public void setDefaultAction(Action action) {68 this.action = action;69 this.actionIsDefault = true;70 }71 72 public void describeTo(Description description) {73 if (! isSatisfied()) {74 description.appendText("! ");75 }76 describeExpectation(description);77 }78 public void describeMismatch(Invocation invocation, Description description) {79 describeExpectation(description);80 final Object[] parameters = invocation.getParametersAsArray();81 if (methodMatcher.matches(invocation.getInvokedMethod()) &&82 parametersMatcher.isCompatibleWith(parameters))83 {84 parametersMatcher.describeMismatch(parameters, description);85 }86 }87 private void describeExpectation(Description description) {88 describeMethod(description);89 parametersMatcher.describeTo(description);90 describeSideEffects(description);91 }92 private void describeMethod(Description description) {93 cardinality.describeTo(description);94 description.appendText(", ");95 if (invocationCount == 0) {96 description.appendText("never invoked");97 }98 else {99 description.appendText("already invoked ");100 description.appendText(Formatting.times(invocationCount));101 }102 description.appendText(": ");103 objectMatcher.describeTo(description);104 description.appendText(".");105 methodMatcher.describeTo(description);106 }107 108 private void describeSideEffects(Description description) {109 for (OrderingConstraint orderingConstraint : orderingConstraints) {110 description.appendText("; ");111 orderingConstraint.describeTo(description);112 }113 114 if (!shouldSuppressActionDescription()) {115 description.appendText("; ");116 action.describeTo(description);117 }118 119 for (SideEffect sideEffect : sideEffects) {120 description.appendText("; ");121 sideEffect.describeTo(description);122 }123 }124 private boolean shouldSuppressActionDescription() {125 return methodIsKnownToBeVoid && actionIsDefault;126 }127 public boolean isSatisfied() {128 return cardinality.isSatisfied(invocationCount);129 }130 131 public boolean allowsMoreInvocations() {132 return cardinality.allowsMoreInvocations(invocationCount);133 }134 135 public boolean matches(Invocation invocation) {136 return allowsMoreInvocations()137 && objectMatcher.matches(invocation.getInvokedObject())138 && methodMatcher.matches(invocation.getInvokedMethod())139 && parametersMatcher.matches(invocation.getParametersAsArray())140 && isInCorrectOrder();141 142 }143 144 private boolean isInCorrectOrder() {145 for (OrderingConstraint constraint : orderingConstraints) {146 if (!constraint.allowsInvocationNow()) return false;147 }148 return true;149 }150 151 public Object invoke(Invocation invocation) throws Throwable {152 invocationCount++;153 performSideEffects();154 final Object result = action.invoke(new Invocation(ExpectationMode.ASSERTING, invocation));155 invocation.checkReturnTypeCompatibility(result);156 return result;157 }158 private void performSideEffects() {159 for (SideEffect sideEffect : sideEffects) {160 sideEffect.perform();161 }162 }163 164 private static class AnyParametersMatcher extends IsAnything<Object[]> implements ParametersMatcher {165 public AnyParametersMatcher() {166 super("(<any parameters>)");167 }168 public boolean isCompatibleWith(Object[] parameters) {169 return true;170 }171 };172}...

Full Screen

Full Screen

Source:ParametersMatcher.java Github

copy

Full Screen

2import java.util.List;3import org.hamcrest.Matcher;4import org.hamcrest.collection.IsArray;5import org.hamcrest.core.IsEqual;6public class ParametersMatcher extends IsArray<Object> {7 public ParametersMatcher(Object[] expectedValues) {8 super(equalMatchersFor(expectedValues));9 }10 11 @SuppressWarnings("unchecked")12 private static Matcher<Object>[] equalMatchersFor(Object[] expectedValues) {13 Matcher<Object>[] matchers = new Matcher[expectedValues.length];14 for (int i = 0; i < expectedValues.length; i++) {15 matchers[i] = new IsEqual<Object>(expectedValues[i]);16 }17 return matchers;18 }19 20 @SuppressWarnings("unchecked")21 public ParametersMatcher(List<Matcher<?>> parameterMatchers) {22 super(parameterMatchers.toArray(new Matcher[0]));23 }24 @Override25 protected String descriptionStart() {26 return "(";27 }28 29 @Override30 protected String descriptionEnd() {31 return ")";32 }33}...

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import org.jmock.Expectations;3import org.jmock.Mockery;4import org.jmock.api.Invocation;5import org.jmock.lib.action.CustomAction;6import org.jmock.lib.action.ReturnValueAction;7import org.jmock.lib.action.VoidAction;8import org.jmock.lib.action.VoidActionSequence;9import org.jmock.lib.legacy.ClassImposteriser;10import org.jmock.test.unit.support.MethodFactory;11import org.junit.Test;12public class ParametersMatcherAcceptanceTests {13 Mockery context = new Mockery() {{14 setImposteriser(ClassImposteriser.INSTANCE);15 }};16 public void canUseParametersMatcherToMatchOnParameters() {17 final MyInterface mock = context.mock(MyInterface.class);18 context.checking(new Expectations() {{19 allowing (mock).doSomething(with(ParametersMatcher.parameters(1, 2, 3)));20 will(returnValue(1));21 }});22 mock.doSomething(1, 2, 3);23 context.assertIsSatisfied();24 }25 public void canUseParametersMatcherToMatchOnParametersWithCustomMatcher() {26 final MyInterface mock = context.mock(MyInterface.class);27 context.checking(new Expectations() {{28 allowing (mock).doSomething(with(ParametersMatcher.parameters(1, 2, 3)));29 will(returnValue(1));30 }});31 mock.doSomething(1, 2, 3);32 context.assertIsSatisfied();33 }34 public void canUseParametersMatcherToMatchOnParametersWithCustomMatcherAndReturnValue() {35 final MyInterface mock = context.mock(MyInterface.class);36 context.checking(new Expectations() {{37 allowing (mock).doSomething(with(ParametersMatcher.parameters(1, 2, 3)));38 will(returnValue(1));39 }});40 mock.doSomething(1, 2, 3);41 context.assertIsSatisfied();42 }43 public void canUseParametersMatcherToMatchOnParametersWithCustomMatcherAndReturnValueAndVoidAction() {44 final MyInterface mock = context.mock(MyInterface.class);45 context.checking(new Expectations() {{46 allowing (mock).doSomething(with(ParametersMatcher.parameters(1, 2, 3)));47 will(new VoidActionSequence(new VoidAction() {

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.Expectations;3import org.jmock.api.Invocation;4import org.jmock.internal.matcher.ParametersMatcher;5import org.jmock.lib.action.ReturnValueAction;6import org.jmock.lib.legacy.ClassImposteriser;7import java.lang.reflect.Method;8import java.lang.reflect.InvocationTargetException;9import java.lang.reflect.Constructor;10import java.util.ArrayList;11import java.util.List;12import java.util.Arrays;13import java.util.Collections;14import java.util.Comparator;15import java.util.HashMap;16import java.util.Map;17import java.util.Set;18import java.util.HashSet;19import java.util.Iterator;20import java.util.ListIterator;21import org.jmock.api.Action;22import org.jmock.api.Invokable;23import org.jmock.internal.PerThreadState;24import org.jmock.internal.ExpectationBuilder;25import org.jmock.internal.InvocationDispatcher;26import org.jmock.internal.InvocationExpectation;27import org.jmock.internal.InvocationExpectationComparator;28import org.jmock.internal.InvocationExpectationDispatcher;29import org.jmock.internal.InvocationExpectationSet;30import org.jmock.internal.InvocationExpectationState;31import org.jmock.internal.InvocationExpectationTranslator;32import org.jmock.internal.InvocationExpectationVerifier;33import org.jmock.intern

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1package org.jmock.examples;2import org.jmock.Mockery;3import org.jmock.Expectations;4import org.jmock.api.Invocation;5import org.jmock.internal.matcher.ParametersMatcher;6import org.jmock.lib.legacy.ClassImposteriser;7public class ParametersMatcherExample {8 public static void main(String[] args) {9 Mockery context = new Mockery();10 context.setImposteriser(ClassImposteriser.INSTANCE);11 final ParametersMatcherExampleInterface mocked = context.mock(ParametersMatcherExampleInterface.class);12 context.checking(new Expectations() {13 {14 oneOf(mocked).method(with(new ParametersMatcher() {15 public boolean matches(Invocation invocation) {16 Object[] parameters = invocation.getParametersAsArray();17 return parameters.length == 2 && parameters[0].equals("a") && parameters[1].equals("b");18 }19 }));20 }21 });22 mocked.method("a", "b");23 context.assertIsSatisfied();24 }25}26interface ParametersMatcherExampleInterface {27 void method(String a, String b);28}29java -cp .;jmock-2.5.1.jar;objenesis-1.2.jar org.jmock.examples.ParametersMatcherExample30 at org.jmock.internal.InvocationDispatcher.checkExpectations(InvocationDispatcher.java:68)31 at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:52)32 at org.jmock.internal.InvocationDispatcher.access$000(InvocationDispatcher.java:16)33 at org.jmock.internal.InvocationDispatcher$1.run(InvocationDispatcher.java:37)34 at org.jmock.internal.InvocationDispatcher.imposeOrdering(InvocationDispatcher.java:101)35 at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:35)36 at org.jmock.internal.ExpectationBuilder.run(ExpectationBuilder.java:76)37 at org.jmock.internal.ExpectationBuilder.run(ExpectationBuilder.java:65)38 at org.jmock.internal.State.run(State.java:128)39 at org.jmock.internal.State.run(State.java:118)40 at org.jmock.Mockery.assertIsSatisfied(Mockery.java:190)41 at org.jmock.examples.ParametersMatcherExample.main(ParametersMatcherExample.java:28)

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.internal.matcher;2import org.jmock.api.Invocation;3import org.jmock.internal.matcher.ParametersMatcher;4import org.jmock.test.unit.support.MethodFactory;5import org.junit.Test;6import java.lang.reflect.Method;7import java.util.Arrays;8import java.util.List;9import static org.hamcrest.Matchers.contains;10import static org.hamcrest.Matchers.equalTo;11import static org.hamcrest.Matchers.is;12import static org.junit.Assert.assertThat;13public class ParametersMatcherTest {14 private final MethodFactory methodFactory = new MethodFactory();15 private final Method method = methodFactory.newMethod("method", String.class, int.class);16 private final Invocation invocation = new Invocation("INVOCATION", method, new Object[]{"hello", 5});17 public void matchesInvocationWithMatchingParameters() {18 ParametersMatcher matcher = new ParametersMatcher(equalTo("hello"), equalTo(5));19 assertThat(matcher.matches(invocation), is(true));20 }21 public void doesNotMatchInvocationWithNonMatchingParameters() {22 ParametersMatcher matcher = new ParametersMatcher(equalTo("hello"), equalTo(6));23 assertThat(matcher.matches(invocation), is(false));24 }25 public void doesNotMatchInvocationWithDifferentNumberOfParameters() {26 ParametersMatcher matcher = new ParametersMatcher(equalTo("hello"), equalTo(5), equalTo(6));27 assertThat(matcher.matches(invocation), is(false));28 }29 public void describesItself() {30 ParametersMatcher matcher = new ParametersMatcher(equalTo("hello"), equalTo(5));31 assertThat(matcher.describeTo(Arrays.asList("DESCRIPTION")), contains("DESCRIPTION", "with parameters [\"hello\", 5]"));32 }33 public void describesMismatch() {34 ParametersMatcher matcher = new ParametersMatcher(equalTo("hello"), equalTo(6));35 assertThat(matcher.describeMismatch(invocation, Arrays.asList("DESCRIPTION")), contains("DESCRIPTION", "parameter 1 was <\"hello\">", "parameter 2 was <5>"));36 }37 public void hasAReadableToString() {38 ParametersMatcher matcher = new ParametersMatcher(equalTo("hello"), equalTo(5));39 assertThat(matcher.toString(), is("with parameters [\"hello\", 5]"));40 }41}42package org.jmock.test.unit.internal.matcher;43import org.jmock.api.Action;44import org

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.internal.matcher.ParametersMatcher;2import org.jmock.core.Invocation;3import org.jmock.core.constraint.IsEqual;4import org.jmock.core.constraint.IsAnything;5import org.jmock.core.constraint.IsSame;6import org.jmock.core.constraint.IsNot;7import org.jmock.core.constraint.IsNotSame;8import org.jmock.core.constraint.IsIn;9import org.jmock.core.constraint.IsNotIn;10import org.jmock.core.constraint.IsInstanceOf;11import org.jmock.core.constraint.IsNotInstanceOf;12import org.jmock.core.constraint.IsNull;13import org.jmock.core.constraint.IsNotNull;14import org.jmock.core.constraint.IsLessThan;15import org.jmock.core.constraint.IsLessOrEqual;16import org.jmock.core.constraint.IsGreaterThan;17import org.jmock.core.constraint.IsGreaterOrEqual;18import org.jmock.core.constraint.IsBetween;19import org.jmock.core.constraint.IsNotBetween;20import org.jmock.core.constraint.IsRegexp;21import org.jmock.core.constraint.IsNotRegexp;22import org.jmock.core.constraint.IsCollectionContaining;23import org.jmock.core.constraint.IsNotCollectionContaining;24import org.jmock.core.constraint.IsMapContaining;25import org.jmock.core.constraint.IsNotMapContaining;26import org.jmock.core.constraint.IsStringStarting;27import org.jmock.core.constraint.IsStringEnding;28import org.jmock.core.constraint.IsStringContaining;29import org.jmock.core.constraint.IsStringNotContaining;30import org.jmock.core.constraint.IsArrayContaining;31import org.jmock.core.constraint.IsArrayNotContaining;32import org.jmock.core.constraint.IsArrayMatching;33import org.jmock.core.constraint.IsArrayNotMatching;34import org.jmock.core.constraint.IsArrayOrdered;35import org.jmock.core.constraint.IsArrayNotOrdered;36import org.jmock.core.constraint.IsArrayEqual;37import org.jmock.core.constraint.IsArrayNotEqual;38import org.jmock.core.constraint.IsArrayExactlyEqual;39import org.jmock.core.constraint.IsArrayNotExactlyEqual;40import org.jmock.core.constraint.IsArrayMatchingInAnyOrder;41import org.jmock.core.constraint.IsArrayNotMatchingInAnyOrder;42import org.jmock.core.constraint.IsArrayEmpty;43import org.jmock.core.constraint.IsArrayNotEmpty;44import org.jmock.core.constraint.IsArrayUnique;45import org.jmock.core.constraint.IsArrayNotUnique;46import org.jmock.core.constraint.IsArraySubset;47import org.jmock.core.constraint.IsArrayNotSubset;48import org.jmock.core.constraint.IsArrayOrderedSubset;49import org.jmock.core.constraint.IsArrayNot

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.unit.internal;2import java.lang.reflect.Method;3import org.jmock.internal.matcher.ParametersMatcher;4import org.jmock.test.unit.support.MethodFactory;5import junit.framework.TestCase;6public class ParametersMatcherTest extends TestCase {7 Method method = MethodFactory.method("aMethod", new Class[] { String.class, int.class, Object.class });8 public void testMatchesWhenParametersAreEqual() throws Exception {9 Object[] arguments = new Object[] { "aString", new Integer(1), new Object() };10 assertTrue("should match", new ParametersMatcher(arguments).matches(method, arguments));11 }12 public void testDoesNotMatchWhenParametersAreNotEqual() throws Exception {13 Object[] arguments = new Object[] { "aString", new Integer(1), new Object() };14 Object[] otherArguments = new Object[] { "aString", new Integer(1), new Object() };15 assertFalse("should not match", new ParametersMatcher(arguments).matches(method, otherArguments));16 }17 public void testDoesNotMatchWhenMethodHasDifferentNumberOfParameters() throws Exception {18 Object[] arguments = new Object[] { "aString", new Integer(1), new Object() };19 assertFalse("should not match", new ParametersMatcher(arguments).matches(method, new Object[0]));20 }21 public void testDoesNotMatchWhenMethodHasDifferentParameterTypes() throws Exception {22 Object[] arguments = new Object[] { "aString", new Integer(1), new Object() };23 Method otherMethod = MethodFactory.method("aMethod", new Class[] { String.class, int.class, String.class });24 assertFalse("should not match", new ParametersMatcher(arguments).matches(otherMethod, arguments));25 }26}27package org.jmock.test.unit.internal;28import java.lang.reflect.Method;29import org.jmock.api.Invocation;30import org.jmock.internal.matcher.ParametersMatcher;31import org.jmock.test.unit.support.MethodFactory;32import junit.framework.TestCase;33public class ParametersMatcherTest extends TestCase {34 Method method = MethodFactory.method("aMethod", new Class[] { String.class, int.class, Object.class });35 public void testMatchesWhenParametersAreEqual() throws Exception {36 Object[] arguments = new Object[] { "aString", new Integer(1), new Object() };

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.api.Invocation;2import org.jmock.internal.matcher.ParametersMatcher;3import org.jmock.lib.action.ReturnValueAction;4class Test{5 public static void main(String[] args) {6 ParametersMatcher pm = new ParametersMatcher(new ReturnValueAction("Hello"));7 Invocation i = new Invocation("Hello", "Hello", new Object[] { "Hello" }, new Class[] { String.class });8 System.out.println(pm.matches(i));9 }10}

Full Screen

Full Screen

ParametersMatcher

Using AI Code Generation

copy

Full Screen

1import org.jmock.Mockery;2import org.jmock.Expectations;3import org.jmock.api.Invocation;4import org.jmock.internal.matcher.ParametersMatcher;5import org.jmock.lib.legacy.ClassImposteriser;6public class ParametersMatcherDemo {7 public static void main(String[] args) {8 Mockery context = new Mockery();9 context.setImposteriser(ClassImposteriser.INSTANCE);10 final ParametersMatcherDemoInterface mock = context.mock(ParametersMatcherDemoInterface.class);11 context.checking(new Expectations() {12 {13 oneOf(mock).method1(with(any(int.class)), with(any(String.class)));14 will(new ParametersMatcher(1, "abc"));15 }16 });17 mock.method1(1, "abc");18 }19}20interface ParametersMatcherDemoInterface {21 void method1(int i, String s);22}

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.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful