Best Jmock-library code snippet using org.jmock.internal.ObjectMethodExpectationBouncer
Source:Mockery.java
...19import org.jmock.internal.InvocationDispatcher;20import org.jmock.internal.InvocationDiverter;21import org.jmock.internal.InvocationToExpectationTranslator;22import org.jmock.internal.NamedSequence;23import org.jmock.internal.ObjectMethodExpectationBouncer;24import org.jmock.internal.ProxiedObjectIdentity;25import org.jmock.internal.ReturnDefaultValueAction;26import org.jmock.internal.SingleThreadedPolicy;27import org.jmock.lib.CamelCaseNamingScheme;28import org.jmock.lib.IdentityExpectationErrorTranslator;29import org.jmock.lib.JavaReflectionImposteriser;30import org.jmock.lib.concurrent.Synchroniser;31/**32 * A Mockery represents the context, or neighbourhood, of the object(s) under test. 33 * 34 * The neighbouring objects in that context are mocked out. The test specifies the 35 * expected interactions between the object(s) under test and its neighbours and 36 * the Mockery checks those expectations while the test is running.37 * 38 * @author npryce39 * @author smgf40 * @author named by Ivan Moore.41 */42public class Mockery implements SelfDescribing {43 private Set<String> mockNames = new HashSet<String>();44 private Imposteriser imposteriser = JavaReflectionImposteriser.INSTANCE;45 private ExpectationErrorTranslator expectationErrorTranslator = IdentityExpectationErrorTranslator.INSTANCE;46 private MockObjectNamingScheme namingScheme = CamelCaseNamingScheme.INSTANCE;47 private ThreadingPolicy threadingPolicy = new SingleThreadedPolicy();48 49 private ReturnDefaultValueAction defaultAction = new ReturnDefaultValueAction(imposteriser);50 51 private InvocationDispatcher dispatcher = new InvocationDispatcher();52 private Error firstError = null;53 54 private List<Invocation> actualInvocations = new ArrayList<Invocation>();55 56 57 /* 58 * Policies59 */60 61 /**62 * Sets the result returned for the given type when no return value has been explicitly63 * specified in the expectation.64 * 65 * @param type66 * The type for which to return <var>result</var>.67 * @param result68 * The value to return when a method of return type <var>type</var>69 * is invoked for which an explicit return value has has not been specified.70 */71 public void setDefaultResultForType(Class<?> type, Object result) {72 defaultAction.addResult(type, result);73 }74 75 /**76 * Changes the imposteriser used to adapt mock objects to the mocked type.77 * 78 * The default imposteriser allows a test to mock interfaces but not79 * classes, so you'll have to plug a different imposteriser into the80 * Mockery if you want to mock classes.81 */82 public void setImposteriser(Imposteriser imposteriser) {83 this.imposteriser = imposteriser;84 this.defaultAction.setImposteriser(imposteriser);85 }86 87 /**88 * Changes the naming scheme used to generate names for mock objects that 89 * have not been explicitly named in the test.90 * 91 * The default naming scheme names mock objects by lower-casing the first92 * letter of the class name, so a mock object of type BananaSplit will be93 * called "bananaSplit" if it is not explicitly named in the test.94 */95 public void setNamingScheme(MockObjectNamingScheme namingScheme) {96 this.namingScheme = namingScheme;97 }98 99 /**100 * Changes the expectation error translator used to translate expectation101 * errors into errors that report test failures.102 * 103 * By default, expectation errors are not translated and are thrown as104 * errors of type {@link ExpectationError}. Plug in a new expectation error105 * translator if you want your favourite test framework to report expectation 106 * failures using its own error type.107 */108 public void setExpectationErrorTranslator(ExpectationErrorTranslator expectationErrorTranslator) {109 this.expectationErrorTranslator = expectationErrorTranslator;110 }111 112 /**113 * Changes the policy by which the Mockery copes with multiple threads.114 * 115 * The default policy throws an exception if the Mockery is called from different116 * threads.117 * 118 * @see Synchroniser119 */120 public void setThreadingPolicy(ThreadingPolicy threadingPolicy) {121 this.threadingPolicy = threadingPolicy;122 }123 124 /*125 * API126 */127 128 /**129 * Creates a mock object of type <var>typeToMock</var> and generates a name for it.130 */131 public <T> T mock(Class<T> typeToMock) {132 return mock(typeToMock, namingScheme.defaultNameFor(typeToMock));133 }134 135 /**136 * Creates a mock object of type <var>typeToMock</var> with the given name.137 */138 public <T> T mock(Class<T> typeToMock, String name) {139 if (mockNames.contains(name)) {140 throw new IllegalArgumentException("a mock with name " + name + " already exists");141 }142 143 MockObject mock = new MockObject(typeToMock, name);144 mockNames.add(name);145 146 Invokable invokable =147 threadingPolicy.synchroniseAccessTo(148 new ProxiedObjectIdentity(149 new InvocationDiverter<CaptureControl>(150 CaptureControl.class, mock, mock)));151 152 return imposteriser.imposterise(invokable, typeToMock, CaptureControl.class);153 }154 155 /** 156 * Returns a new sequence that is used to constrain the order in which 157 * expectations can occur.158 * 159 * @param name160 * The name of the sequence.161 * @return162 * A new sequence with the given name.163 */164 public Sequence sequence(String name) {165 return new NamedSequence(name);166 }167 168 /** 169 * Returns a new state machine that is used to constrain the order in which 170 * expectations can occur.171 * 172 * @param name173 * The name of the state machine.174 * @return175 * A new state machine with the given name.176 */177 public States states(String name) {178 return dispatcher.newStateMachine(name);179 }180 181 /**182 * Specifies the expected invocations that the object under test will perform upon183 * objects in its context during the test.184 * 185 * The builder is responsible for interpreting high-level, readable API calls to 186 * construct an expectation.187 * 188 * This method can be called multiple times per test and the expectations defined in189 * each block are combined as if they were defined in same order within a single block.190 */191 public void checking(ExpectationBuilder expectations) {192 expectations.buildExpectations(defaultAction, dispatcher);193 }194 195 /**196 * Adds an expected invocation that the object under test will perform upon197 * objects in its context during the test.198 * 199 * This method allows a test to define an expectation explicitly, bypassing the200 * high-level API, if desired.201 */202 public void addExpectation(Expectation expectation) {203 dispatcher.add(expectation);204 }205 206 /**207 * Fails the test if there are any expectations that have not been met.208 */209 public void assertIsSatisfied() {210 if (firstError != null) {211 throw firstError;212 }213 else if (!dispatcher.isSatisfied()) {214 throw expectationErrorTranslator.translate(215 new ExpectationError("not all expectations were satisfied", this));216 }217 }218 219 public void describeTo(Description description) {220 description.appendDescriptionOf(dispatcher);221 describeHistory(description);222 }223 private void describeMismatch(Invocation invocation, Description description) {224 dispatcher.describeMismatch(invocation, description);225 describeHistory(description);226 }227 228 private void describeHistory(Description description) {229 description.appendText("\nwhat happened before this:");230 231 if (actualInvocations.isEmpty()) {232 description.appendText(" nothing!");233 }234 else {235 description.appendList("\n ", "\n ", "\n", actualInvocations);236 }237 }238 private Object dispatch(Invocation invocation) throws Throwable {239 if (firstError != null) {240 throw firstError;241 }242 243 try {244 Object result = dispatcher.dispatch(invocation);245 actualInvocations.add(invocation);246 return result;247 }248 catch (ExpectationError e) {249 firstError = expectationErrorTranslator.translate(mismatchDescribing(e));250 firstError.setStackTrace(e.getStackTrace());251 throw firstError;252 }253 catch (Throwable t) {254 actualInvocations.add(invocation);255 throw t;256 }257 }258 259 private ExpectationError mismatchDescribing(final ExpectationError e) {260 ExpectationError filledIn = new ExpectationError(e.getMessage(), new SelfDescribing() {261 public void describeTo(Description description) {262 describeMismatch(e.invocation, description);263 }264 }, e.invocation);265 filledIn.setStackTrace(e.getStackTrace());266 return filledIn;267 }268 private class MockObject implements Invokable, CaptureControl {269 private Class<?> mockedType;270 private String name;271 272 public MockObject(Class<?> mockedType, String name) {273 this.name = name;274 this.mockedType = mockedType;275 }276 277 @Override278 public String toString() {279 return name;280 }281 282 public Object invoke(Invocation invocation) throws Throwable {283 return dispatch(invocation);284 }285 public Object captureExpectationTo(ExpectationCapture capture) {286 return imposteriser.imposterise(287 new ObjectMethodExpectationBouncer(new InvocationToExpectationTranslator(capture, defaultAction)), 288 mockedType);289 }290 }291}
Source:ObjectMethodExpectationBouncer.java
1package org.jmock.internal;2import org.jmock.api.Invokable;3public class ObjectMethodExpectationBouncer extends FakeObjectMethods {4 public ObjectMethodExpectationBouncer(Invokable next) {5 super(next);6 }7 @Override8 protected boolean fakeEquals(Object invokedObject, Object other) {9 throw cannotDefineExpectation();10 }11 @Override12 protected void fakeFinalize(Object invokedObject) {13 throw cannotDefineExpectation();14 }15 @Override16 protected int fakeHashCode(Object invokedObject) {17 throw cannotDefineExpectation();18 }...
ObjectMethodExpectationBouncer
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.DynamicMockError;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.IsSame;9import org.jmock.core.constraint.IsInstanceOf;10import org.jmock.core.constraint.IsAnything;11import org.jmock.core.constraint.IsCollectionContaining;12import org.jmock.core.constraint.IsArrayContaining;13import org.jmock.core.constraint.IsArrayContainingInOrder;14import org.jmock.core.constraint.IsArrayContainingInAnyOrder;15import org.jmock.core.constraint.IsArrayMatching;16import org.jmock.core.constraint.IsCollectionContainingInOrder;17import org.jmock.core.constraint.IsCollectionContainingInAnyOrder;18import org.jmock.core.constraint.IsCollectionMatching;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.IsNot;25import org.jmock.core.constraint.IsIn;26import org.jmock.core.constraint.IsNotIn;27import org.jmock.core.constraint.IsLessThan;28import org.jmock.core.constraint.IsGreaterThan;29import org.jmock.core.constraint.IsLessThanOrEqualTo;30import org.jmock.core.constraint.IsGreaterThanOrEqualTo;31import org.jmock.core.constraint.IsComparableEqualTo;32import org.jmock.core.constraint.IsComparableNotEqualTo;33import org.jmock.core.constraint.IsComparableLessThan;34import org.jmock.core.constraint.IsComparableGreaterThan;35import org.jmock.core.constraint.IsComparableLessThanOrEqualTo;36import org.jmock.core.constraint.IsComparableGreaterThanOrEqualTo;37import org.jmock.core.constraint.IsMapContaining;38import org.jmock.core.constraint.IsMapContainingKey;39import org.jmock.core.constraint.IsMapContainingValue;40import org.jmock.core.constraint.IsMapContainingInOrder;41import org.jmock.core.constraint.IsMapContainingInAnyOrder;42import org.jmock.core.constraint.IsMapMatching;43import org.jmock.core.constraint.IsSame;44import org.jmock.core.constraint.IsEqual;45import org.jmock.core.constraint.IsNull;46import org.jmock.core.constraint.IsSame;47import org.jmock.core.constraint.IsEqual;48import org.jmock.core.constraint.IsNull;49import org.jmock.core.constraint.IsSame;50import org.jmock.core.constraint.IsEqual;51import org.jmock.core.constraint.IsNull;
ObjectMethodExpectationBouncer
Using AI Code Generation
1import org.jmock.MockObjectTestCase;2import org.jmock.Mock;3import org.jmock.core.Invocation;4import org.jmock.core.Constraint;5import org.jmock.core.DynamicMockError;6import org.jmock.core.InvocationMatcher;7import org.jmock.core.Invokable;8import org.jmock.core.Stub;9import org.jmock.core.StubConstraint;10import org.jmock.core.StubConstraintList;11import org.jmock.core.StubSequence;12import org.jmock.core.StubSequenceList;13import org.jmock.core.StubSequenceListIterator;14import org.jmock.core.StubSequenceListIteratorFactory;15import org.jmock.core.StubSequenceListIteratorFactoryImpl;16import org.jmock.core.StubSequenceListIteratorImpl;17import org.jmock.core.StubSequenceListIteratorImplFactory;18import org.jmock.core.StubSequenceListIteratorImplFactoryImpl;19import org.jmock.core.StubSequenceListIteratorImplFactoryImpl2;20import org.jmock.core.StubSequenceListIteratorImplFactoryImpl3;21import org.jmock.core.StubSequenceListIteratorImplFactoryImpl4;22import org.jmock.core.StubSequenceListIteratorImplFactoryImpl5;23import org.jmock.core.StubSequenceListIteratorImplFactoryImpl6;24import org.jmock.core.StubSequenceListIteratorImplFactoryImpl7;25import org.jmock.core.StubSequenceListIteratorImplFactoryImpl8;26import org.jmock.core.StubSequenceListIteratorImplFactoryImpl9;27import org.jmock.core.StubSequenceListIteratorImplFactoryImpl10;28import org.jmock.core.StubSequenceListIteratorImplFactoryImpl11;29import org.jmock.core.StubSequenceListIteratorImplFactoryImpl12;30import org.jmock.core.StubSequenceListIteratorImplFactoryImpl13;31import org.jmock.core.StubSequenceListIteratorImplFactoryImpl14;32import org.jmock.core.StubSequenceListIteratorImplFactoryImpl15;33import org.jmock.core.StubSequenceListIteratorImplFactoryImpl16;34import org.jmock.core.StubSequenceListIteratorImplFactoryImpl17;35import org.jmock.core.StubSequenceListIteratorImplFactoryImpl18;36import org.jmock.core.StubSequenceListIteratorImplFactoryImpl19;37import org.jmock.core.StubSequenceListIteratorImplFactoryImpl20;38import org.jmock.core.StubSequenceListIteratorImplFactoryImpl21;39import org.jmock.core.StubSequenceListIteratorImplFactoryImpl22;40import org.jmock.core.Stub
ObjectMethodExpectationBouncer
Using AI Code Generation
1import org.jmock.core.*;2import org.jmock.core.constraint.*;3import org.jmock.core.matcher.*;4import org.jmock.core.stub.*;5import org.jmock.core.Invocation;6import org.jmock.core.Stub;7import org.jmock.core.StubSequence;8import org.jmock.core.constraint.IsEqual;9import org.jmock.core.constraint.IsAnything;10import org.jmock.core.constraint.IsEqual;11import org.jmock.core.constraint.IsInstanceOf;12import org.jmock.core.constraint.IsSame;13import org.jmock.core.constraint.IsNot;14import org.jmock.core.constraint.IsCollectionContaining;15import org.jmock.core.constraint.IsSubstring;16import org.jmock.core.constraint.IsIn;17import org.jmock.core.constraint.IsNotIn;18import org.jmock.core.constraint.IsGreaterThan;19import org.jmock.core.constraint.IsLessThan;20import org.jmock.core.constraint.IsBetween;21import org.jmock.core.constraint.IsCloseTo;22import org.jmock.core.constraint.IsArrayContaining;23import org.jmock.core.constraint.IsMapContaining;24import org.jmock.core.constraint.IsRegExp;25import org.jmock.core.constraint.IsThrowableMessage;26import org.jmock.core.constraint.IsThrowableCause;27import org.jmock.core.constraint.IsThrowableClass;28import org.jmock.core.constraint.IsThrowableMessage;29import org.jmock.core.constraint.IsThrowableCause;30import org.jmock.core.constraint.IsThrowableClass;31import org.jmock.core.constraint.Is;32import org.jmock.core.constraint.Or;33import org.jmock.core.constraint.And;34import org.jmock.core.constraint.Not;35import org.jmock.core.constraint.Xor;36import org.jmock.core.constraint.RegExp;37import org.jmock.core.constraint.StringContains;38import org.jmock.core.constraint.IsEqual;39import org.jmock.core.constraint.IsInstanceOf;40import org.jmock.core.constraint.IsSame;41import org.jmock.core.constraint.IsNot;42import org.jmock.core.constraint.IsCollectionContaining;43import org.jmock.core.constraint.IsSubstring;44import org.jmock.core.constraint.IsIn;45import org.jmock.core.constraint.IsNotIn;46import org.jmock.core.constraint.IsGreaterThan;47import org.jmock.core.constraint.IsLessThan;48import org.jmock.core.constraint.IsBetween;49import org.jmock.core.constraint.IsCloseTo;50import org.jmock.core.constraint.IsArrayContaining;51import org.jmock.core.constraint.IsMapContaining;52import org.jmock.core.constraint.IsRegExp;53import org.jmock.core.constraint.IsThrowableMessage;54import org.jmock.core.constraint.IsThrowableCause;55import org.jmock.core.constraint.Is
ObjectMethodExpectationBouncer
Using AI Code Generation
1import org.jmock.*;2import org.jmock.core.*;3import org.jmock.internal.*;4import org.jmock.core.constraint.*;5import org.jmock.core.matcher.*;6import org.jmock.core.stub.*;7import org.jmock.util.*;8public class 1 {9 public static void main(String[] args) {10 Mock mock = new Mock(Object.class);11 ObjectMethodExpectationBouncer bouncer = new ObjectMethodExpectationBouncer(mock);12 bouncer.bounce(new Method("toString", new Class[]{}));13 mock.verify();14 }15}16java.lang.AssertionError: Expected toString() to be invoked once, but was invoked 0 times17 at org.jmock.Mock.verify(Mock.java:186)18 at 1.main(1.java:15)19import org.jmock.*;20import org.jmock.core.*;21import org.jmock.internal.*;22import org.jmock.core.constraint.*;23import org.jmock.core.matcher.*;24import org.jmock.core.stub.*;25import org.jmock.util.*;26public class 2 {27 public static void main(String[] args) {28 Mock mock = new Mock(Object.class);29 ObjectMethodExpectationBouncer bouncer = new ObjectMethodExpectationBouncer(mock);30 bouncer.bounce(new Method("toString", new Class[]{}));31 mock.verify();32 }33}34java.lang.AssertionError: Expected toString() to be invoked once, but was invoked 0 times35 at org.jmock.Mock.verify(Mock.java:186)36 at 2.main(2.java:15)37import org.jmock.*;38import org.jmock.core.*;39import org.jmock.internal.*;40import org.jmock.core.constraint.*;41import org.jmock.core.matcher.*;42import org.jmock.core.stub.*;43import org.jmock.util.*;44public class 3 {45 public static void main(String[] args) {46 Mock mock = new Mock(Object.class);47 ObjectMethodExpectationBouncer bouncer = new ObjectMethodExpectationBouncer(mock);48 bouncer.bounce(new Method("toString", new Class[]{}));49 mock.verify();50 }51}52java.lang.AssertionError: Expected toString() to be invoked once, but was invoked 0 times53 at org.jmock.Mock.verify(Mock
ObjectMethodExpectationBouncer
Using AI Code Generation
1import org.jmock.*;2import org.jmock.core.*;3import org.jmock.internal.*;4import org.jmock.util.*;5public class 1 {6 public static void main(String[] args) {7 Mock mock = new Mock(Object.class);8 ObjectMethodExpectationBouncer bouncer = new ObjectMethodExpectationBouncer(mock);9 bouncer.bounce(new Method("toString"));10 }11}12 at org.jmock.internal.ObjectMethodExpectationBouncer.bounce(ObjectMethodExpectationBouncer.java:34)13 at 1.main(1.java:13)14import org.jmock.*;15import org.jmock.core.*;16import org.jmock.internal.*;17import org.jmock.util.*;18public class 2 {19 public static void main(String[] args) {20 Mock mock = new Mock(Object.class);21 ObjectMethodExpectationBouncer bouncer = new ObjectMethodExpectationBouncer(mock);22 bouncer.bounce(new Method("hashCode"));23 }24}25 at org.jmock.internal.ObjectMethodExpectationBouncer.bounce(ObjectMethodExpectationBouncer.java:34)26 at 2.main(2.java:13)27import org.jmock.*;28import org.jmock.core.*;29import org.jmock.internal.*;30import org.jmock.util.*;31public class 3 {32 public static void main(String[] args) {33 Mock mock = new Mock(Object.class);34 ObjectMethodExpectationBouncer bouncer = new ObjectMethodExpectationBouncer(mock);35 bouncer.bounce(new Method("equals"));36 }37}
ObjectMethodExpectationBouncer
Using AI Code Generation
1import org.jmock.Mockery;2import org.jmock.api.Invocation;3import org.jmock.api.Invokable;4import org.jmock.internal.ObjectMethodExpectationBouncer;5import org.jmock.lib.legacy.ClassImposteriser;6public class 1 {7public static void main(String[] args) {8Mockery context = new Mockery();9context.setImposteriser(ClassImposteriser.INSTANCE);10ObjectMethodExpectationBouncer mock = context.mock(ObjectMethodExpectationBouncer.class);11context.checking(new Expectations() {12{13oneOf(mock).invoke(with(any(Invocation.class)));14}15});16mock.invoke(new Invocation() {17public Object invoke(Invokable invokable, Object... params) throws Throwable {18System.out.println("Invoked");19return null;20}21});22context.assertIsSatisfied();23}24}
ObjectMethodExpectationBouncer
Using AI Code Generation
1import java.util.*;2import org.jmock.*;3import org.jmock.core.*;4import org.jmock.util.*;5import org.jmock.internal.*;6public class 1 {7 public static void main(String[] args) {8 MockObject mo = new MockObject("mock");9 Mock mock = mo.getMock();10 mock.expects(new ObjectMethodExpectationBouncer("someMethod", new Object[] {new Integer(1), new Integer(2), new Integer(3)}, new Integer(6)));11 System.out.println("Expectation created");12 }13}
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!!