Best Jmock-library code snippet using org.jmock.internal.InvocationToExpectationTranslator.invoke
Source:Mockery.java
...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:InvocationToExpectationTranslator.java
...13 this.capture = capture;14 this.defaultAction = defaultAction;15 }16 17 public Object invoke(Invocation invocation) throws Throwable {18 Invocation buildingInvocation = new Invocation(ExpectationMode.BUILDING,invocation);19 capture.createExpectationFrom(buildingInvocation);20 return defaultAction.invoke(buildingInvocation);21 }22}...
invoke
Using AI Code Generation
1import org.jmock.Mock;2import org.jmock.MockObjectTestCase;3import org.jmock.core.Invocation;4import org.jmock.core.InvocationDispatcher;5import org.jmock.core.InvocationToExpectationTranslator;6import org.jmock.core.Stub;7import org.jmock.core.StubObject;8import org.jmock.core.constraint.IsAnything;9import org.jmock.core.constraint.IsEqual;10import org.jmock.core.constraint.IsSame;11import org.jmock.core.constraint.StringContains;12import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;13import org.jmock.core.matcher.InvokeAtMostOnceMatcher;14import org.jmock.core.matcher.InvokeCountMatcher;15import org.jmock.core.matcher.InvokeExactlyOnceMatcher;16import org.jmock.core.matcher.InvokeOnceAndNoMoreMatcher;17import org.jmock.core.matcher.InvokeOnceMatcher;18import org.jmock.core.matcher.InvokeOnceOrMoreMatcher;19import org.jmock.core.matcher.InvokeOnceOrNeverMatcher;20import org.jmock.core.matcher.InvokeOnceOrTwiceMatcher;21import org.jmock.core.matcher.InvokeTwiceMatcher;22import org.jmock.core.matcher.InvokeTwiceOrMoreMatcher;23import org.jmock.core.stub.ReturnStub;24import org.jmock.core.stub.ThrowStub;25import org.jmock.core.stub.VoidStub;26import org.jmock.expectation.AssertMo;27import org.jmock.expectation.ExpectationCounter;28import org.jmock.expectation.ExpectationCounterMatcher;29import org.jmock.expectation.ExpectationCounterStub;30import org.jmock.expectation.ExpectationSet;31import org.jmock.expectation.ExpectationValue;32import org.jmock.expectation.ExpectationValueMatcher;33import org.jmock.expectation.ExpectationValueStub;34import org.jmock.expectation.InvocationExpectation;35import org.jmock.expectation.InvocationExpectationSet;36import org.jmock.expectation.InvocationResultMatcher;37import org.jmock.expectation.InvocationStub;38import org.jmock.expectation.ModifierStub;39import org.jmock.expectation.OrderedExpectationSet;40import org.jmock.expectation.ReturnValueModifier;41import org.jmock.expectation.StubModifier;42import org.jmock.expectation.ThrowableModifier;43import org.jmock.expectation.VoidModifier;44import org.jmock.util.Dummy;45import org.jmock.util.DummyThrowable;46public class Test extends MockObjectTestCase {47 public void test() {48 Mock mock = mock(MyInterface.class);49 mock.expects(once()).method("method").with(eq("arg")).will(return
invoke
Using AI Code Generation
1package org.jmock.test.acceptance;2import org.jmock.Expectations;3import org.jmock.Mockery;4import org.jmock.api.Invocation;5import org.jmock.internal.InvocationToExpectationTranslator;6import org.jmock.lib.legacy.ClassImposteriser;7import org.junit.Test;8public class JMock1Test {9 public void testJMock1() {10 Mockery context = new Mockery();11 context.setImposteriser(ClassImposteriser.INSTANCE);12 final Foo foo = context.mock(Foo.class);13 context.checking(new Expectations() {14 {15 oneOf(foo).bar();16 }17 });18 foo.bar();19 }20 public interface Foo {21 void bar();22 }23}24package org.jmock.test.acceptance;25import org.jmock.Expectations;26import org.jmock.Mockery;27import org.jmock.api.Invocation;28import org.jmock.internal.InvocationToExpectationTranslator;29import org.jmock.lib.legacy.ClassImposteriser;30import org.junit.Test;31public class JMock1Test {32 public void testJMock1() {33 Mockery context = new Mockery();34 context.setImposteriser(ClassImposteriser.INSTANCE);35 final Foo foo = context.mock(Foo.class);36 context.checking(new Expectations() {37 {38 oneOf(foo).bar();39 }40 });41 foo.bar();42 }43 public interface Foo {44 void bar();45 }46}47package org.jmock.test.acceptance;48import org.jmock.Expectations;49import org.jmock.Mockery;50import org.jmock.api.Invocation;51import org.jmock.internal.InvocationToExpectationTranslator;52import org.jmock.lib.legacy.ClassImposteriser;53import org.junit.Test;54public class JMock1Test {55 public void testJMock1() {56 Mockery context = new Mockery();57 context.setImposteriser(ClassImposteriser.INSTANCE);58 final Foo foo = context.mock(Foo.class);59 context.checking(new Expect
invoke
Using AI Code Generation
1package org.jmock.test.acceptance;2import org.jmock.Mockery;3import org.jmock.Expectations;4import org.jmock.integration.junit4.JUnitRuleMockery;5import org.jmock.lib.legacy.ClassImposteriser;6import org.jmock.test.unit.lib.legacy.ClassImposteriserTest;7import org.junit.Rule;8import org.junit.Test;9public class JUnitRuleMockeryAcceptanceTests {10 public Mockery context = new JUnitRuleMockery();11 public ClassImposteriserTest.InterfaceToMock mock = context.mock(ClassImposteriserTest.InterfaceToMock.class);12 public void canUseMockInExpectation() {13 context.checking(new Expectations() {{14 oneOf (mock).methodToMock(); will(returnValue("foo"));15 }});16 mock.methodToMock();17 }18}19package org.jmock.test.acceptance;20import org.jmock.Mockery;21import org.jmock.Expectations;22import org.jmock.integration.junit4.JUnitRuleMockery;23import org.jmock.lib.legacy.ClassImposteriser;24import org.jmock.test.unit.lib.legacy.ClassImposteriserTest;25import org.junit.Rule;26import org.junit.Test;27public class JUnitRuleMockeryAcceptanceTests {28 public Mockery context = new JUnitRuleMockery();29 public ClassImposteriserTest.InterfaceToMock mock = context.mock(ClassImposteriserTest.InterfaceToMock.class);30 public void canUseMockInExpectation() {31 context.checking(new Expectations() {{32 oneOf (mock).methodToMock(); will(returnValue("foo"));33 }});34 mock.methodToMock();35 }36}37package org.jmock.test.acceptance;38import org.jmock.Mockery;39import org.jmock.Expectations;40import org.jmock.integration.junit4.JUnitRuleMockery;41import org.jmock.lib.legacy.ClassImposteriser;42import org.jmock.test.unit.lib.legacy.ClassImposteriserTest;43import org.junit.Rule;
invoke
Using AI Code Generation
1package com.jmock.test;2import org.jmock.Expectations;3import org.jmock.Mockery;4import org.jmock.integration.junit4.JUnit4Mockery;5import org.jmock.internal.InvocationToExpectationTranslator;6import org.jmock.lib.legacy.ClassImposteriser;7import org.junit.Test;8public class JMockTest {9 public void test() {10 Mockery context = new JUnit4Mockery() {11 {12 setImposteriser(ClassImposteriser.INSTANCE);13 }14 };15 final Foo foo = context.mock(Foo.class);16 InvocationToExpectationTranslator translator = new InvocationToExpectationTranslator();17 Expectations expectation = translator.createInvocation("bar", new Object[] {});18 context.checking(expectation);19 foo.bar();20 context.assertIsSatisfied();21 }22}23package com.jmock.test;24import org.jmock.Expectations;25import org.jmock.Mockery;26import org.jmock.integration.junit4.JUnit4Mockery;27import org.jmock.internal.InvocationToExpectation
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!!