How to use Invocation method of org.jmock.api.Invocation class

Best Jmock-library code snippet using org.jmock.api.Invocation.Invocation

Source:UnsynchronisedInvocationDispatcher.java Github

copy

Full Screen

...5import org.hamcrest.Description;6import org.hamcrest.SelfDescribing;7import org.jmock.api.Expectation;8import org.jmock.api.ExpectationError;9import org.jmock.api.Invocation;10import org.jmock.api.InvocationDispatcher;11import org.jmock.internal.StateMachine;12public class UnsynchronisedInvocationDispatcher implements InvocationDispatcher {13 private final Collection<Expectation> expectations;14 private final Collection<StateMachine> stateMachines;15 public UnsynchronisedInvocationDispatcher() {16 expectations = new ArrayList<Expectation>();17 stateMachines = new ArrayList<StateMachine>();18 }19 public UnsynchronisedInvocationDispatcher(Collection<Expectation> theExpectations, Collection<StateMachine> theStateMachines) {20 expectations = theExpectations;21 stateMachines = theStateMachines;22 }23 /* (non-Javadoc)24 * @see org.jmock.internal.InvocationDispatcher#newStateMachine(java.lang.String)25 */26 public StateMachine newStateMachine(String name) {27 StateMachine stateMachine = new StateMachine(name);28 stateMachines.add(stateMachine);29 return stateMachine;30 }31 /* (non-Javadoc)32 * @see org.jmock.internal.InvocationDispatcher#add(org.jmock.api.Expectation)33 */34 public void add(Expectation expectation) {35 expectations.add(expectation);36 }37 /* (non-Javadoc)38 * @see org.jmock.internal.InvocationDispatcher#describeTo(org.hamcrest.Description)39 */40 public void describeTo(Description description) {41 describe(description, expectations);42 }43 /* (non-Javadoc)44 * @see org.jmock.internal.InvocationDispatcher#describeMismatch(org.jmock.api.Invocation, org.hamcrest.Description)45 */46 public void describeMismatch(Invocation invocation, Description description) {47 describe(description, describedWith(expectations, invocation));48 }49 private Iterable<SelfDescribing> describedWith(Iterable<Expectation> expectations, final Invocation invocation) {50 final Iterator<Expectation> iterator = expectations.iterator();51 return new Iterable<SelfDescribing>() {52 public Iterator<SelfDescribing> iterator() {53 return new Iterator<SelfDescribing>() {54 public boolean hasNext() {55 return iterator.hasNext();56 }57 public SelfDescribing next() {58 return new SelfDescribing() {59 public void describeTo(Description description) {60 iterator.next().describeMismatch(invocation, description);61 }62 };63 }64 public void remove() {65 iterator.remove();66 }67 };68 }69 };70 }71 private void describe(Description description, Iterable<? extends SelfDescribing> selfDescribingExpectations) {72 if (expectations.isEmpty()) {73 description.appendText("no expectations specified: did you...\n" +74 " - forget to start an expectation with a cardinality clause?\n" +75 " - call a mocked method to specify the parameter of an expectation?");76 } else {77 description.appendList("expectations:\n ", "\n ", "", selfDescribingExpectations);78 if (!stateMachines.isEmpty()) {79 description.appendList("\nstates:\n ", "\n ", "", stateMachines);80 }81 }82 }83 /* (non-Javadoc)84 * @see org.jmock.internal.InvocationDispatcher#isSatisfied()85 */86 public boolean isSatisfied() {87 for (Expectation expectation : expectations) {88 if (!expectation.isSatisfied()) {89 return false;90 }91 }92 return true;93 }94 /* (non-Javadoc)95 * @see org.jmock.internal.InvocationDispatcher#dispatch(org.jmock.api.Invocation)96 */97 public Object dispatch(Invocation invocation) throws Throwable {98 for (Expectation expectation : expectations) {99 if (expectation.matches(invocation)) {100 return expectation.invoke(invocation);101 }102 }103 throw ExpectationError.unexpected("unexpected invocation", invocation);104 }105}...

Full Screen

Full Screen

Source:Synchroniser.java Github

copy

Full Screen

2import static org.hamcrest.StringDescription.asString;3import java.util.concurrent.CopyOnWriteArrayList;4import java.util.concurrent.TimeoutException;5import org.jmock.api.Expectation;6import org.jmock.api.Invocation;7import org.jmock.api.InvocationDispatcher;8import org.jmock.api.Invokable;9import org.jmock.api.ThreadingPolicy;10import org.jmock.internal.StateMachine;11import org.jmock.internal.StatePredicate;12import org.jmock.lib.concurrent.internal.FixedTimeout;13import org.jmock.lib.concurrent.internal.InfiniteTimeout;14import org.jmock.lib.concurrent.internal.Timeout;15/**16 * A ThreadingPolicy that makes the Mockery thread-safe and helps tests17 * synchronise with background threads.18 * 19 * @author Nat Pryce20 * @author olibye21 */22public class Synchroniser implements ThreadingPolicy {23 private final Object sync = new Object();24 private Error firstError = null;25 private InvocationDispatcher invocationDispatcher;26 public Synchroniser() {27 invocationDispatcher = new UnsynchronisedInvocationDispatcher(28 new CopyOnWriteArrayList<Expectation>(),29 new CopyOnWriteArrayList<StateMachine>());30 }31 public Synchroniser(InvocationDispatcher dispatcher) {32 invocationDispatcher = dispatcher;33 }34 /**35 * Waits for a StatePredicate to become active.36 * 37 * Warning: this will wait forever unless the test itself has a timeout.38 * 39 * @param p40 * the StatePredicate to wait for41 * @throws InterruptedException42 */43 public void waitUntil(StatePredicate p) throws InterruptedException {44 waitUntil(p, new InfiniteTimeout());45 }46 /**47 * Waits up to a timeout for a StatePredicate to become active. Fails the test48 * if the timeout expires.49 * 50 * @param p the StatePredicate to wait for51 * @param timeoutMs52 * the timeout in milliseconds53 * @throws InterruptedException54 */55 public void waitUntil(StatePredicate p, long timeoutMs) throws InterruptedException {56 waitUntil(p, new FixedTimeout(timeoutMs));57 }58 private void waitUntil(StatePredicate p, Timeout timeout) throws InterruptedException {59 synchronized (sync) {60 while (!p.isActive()) {61 try {62 sync.wait(timeout.timeRemaining());63 } catch (TimeoutException e) {64 if (firstError != null) {65 throw firstError;66 } else {67 throw new AssertionError("timed out waiting for " + asString(p));68 }69 }70 }71 }72 }73 public Invokable synchroniseAccessTo(final Invokable mockObject) {74 return new Invokable() {75 public Object invoke(Invocation invocation) throws Throwable {76 return synchroniseInvocation(mockObject, invocation);77 }78 };79 }80 private Object synchroniseInvocation(Invokable mockObject, Invocation invocation) throws Throwable {81 synchronized (sync) {82 try {83 return mockObject.invoke(invocation);84 } catch (Error e) {85 if (firstError == null) {86 firstError = e;87 }88 throw e;89 } finally {90 sync.notifyAll();91 }92 }93 }94 public InvocationDispatcher dispatcher() {95 return invocationDispatcher;96 }97}...

Full Screen

Full Screen

Source:BeanProxy.java Github

copy

Full Screen

1package org.jtester.unitils.jmock;23import java.lang.reflect.InvocationTargetException;4import java.lang.reflect.Method;56import org.jmock.api.Imposteriser;7import org.jmock.api.Invocation;8import org.jmock.api.Invokable;9import org.jmock.lib.legacy.ClassImposteriser;1011public class BeanProxy implements Invokable {12 private String name;1314 private Class<?> type;1516 protected BeanProxy(final String name, final Class<?> type) {17 this.name = name;18 this.type = type;19 }2021 public Object invoke(Invocation invocation) throws Throwable {22 Object mock = MockBeanRegister.getBean(name, type);23 Method method = invocation.getInvokedMethod();24 try {25 return method.invoke(mock, invocation.getParametersAsArray());26 } catch (Throwable e) {27 if (e instanceof InvocationTargetException) {28 throw ((InvocationTargetException) e).getTargetException();29 } else {30 throw e;31 }32 }33 }3435 private static Imposteriser imposteriser = ClassImposteriser.INSTANCE;3637 @SuppressWarnings("unchecked")38 public static <T> T proxy(final String name, final Class<?> type) {39 return (T) imposteriser.imposterise(new BeanProxy(name, type), type);40 }41}

Full Screen

Full Screen

Invocation

Using AI Code Generation

copy

Full Screen

1package org.jmock.test.acceptance;2import junit.framework.*;3import org.jmock.*;4import org.jmock.core.*;5import org.jmock.test.unit.*;6public class InvocationAcceptanceTests extends JUnit4MockeryAcceptanceTest {7 public interface MockType {8 void doSomething();9 }10 public void testCanGetMethodNameFromInvocation() {11 MockType mock = mock(MockType.class, "mock");12 checking(new Expectations() {{13 oneOf (mock).doSomething();14 }});15 mock.doSomething();16 }17 public void testCanGetParametersFromInvocation() {18 final MockType mock = mock(MockType.class, "mock");19 checking(new Expectations() {{20 oneOf (mock).doSomething();21 }});22 mock.doSomething();23 }24 public void testCanGetParameterTypesFromInvocation() {25 final MockType mock = mock(MockType.class, "mock");26 checking(new Expectations() {{27 oneOf (mock).doSomething();28 }});29 mock.doSomething();30 }31 public void testCanGetParameterNamesFromInvocation() {32 final MockType mock = mock(MockType.class, "mock");33 checking(new Expectations() {{34 oneOf (mock).doSomething();35 }});36 mock.doSomething();37 }38 public void testCanGetParameterValuesFromInvocation() {39 final MockType mock = mock(MockType.class, "mock");40 checking(new Expectations() {{41 oneOf (mock).doSomething();42 }});43 mock.doSomething();44 }45}

Full Screen

Full Screen

Invocation

Using AI Code Generation

copy

Full Screen

1public class 1 {2 public void testMethod() {3 Mockery context = new JUnit4Mockery();4 final ClassToMock mock = context.mock(ClassToMock.class);5 context.checking(new Expectations() {6 {7 ignoring(mock).method1();8 ignoring(mock).method2();9 ignoring(mock).method3();10 }11 });12 mock.method1();13 mock.method2();14 mock.method3();15 Invocation invocation = context.getInvocations().iterator().next();16 String methodName = invocation.getInvokedMethod().getName();17 assertEquals("method1", methodName);18 }19}20public class 2 {21 public void testMethod() {22 Mockery context = new JUnit4Mockery();23 final ClassToMock mock = context.mock(ClassToMock.class);24 context.checking(new Expectations() {25 {26 ignoring(mock).method1();27 ignoring(mock).method2();28 ignoring(mock).method3();29 }30 });31 mock.method1();32 mock.method2();33 mock.method3();34 Invocation invocation = context.getInvocations().iterator().next();35 String methodName = invocation.getInvokedMethod().getName();36 assertEquals("method1", methodName);37 }38}39public class 3 {40 public void testMethod() {41 Mockery context = new JUnit4Mockery();42 final ClassToMock mock = context.mock(ClassToMock.class);43 context.checking(new Expectations() {44 {45 ignoring(mock).method1();46 ignoring(mock).method2();47 ignoring(mock).method3();48 }49 });50 mock.method1();51 mock.method2();52 mock.method3();53 Invocation invocation = context.getInvocations().iterator().next();54 String methodName = invocation.getInvokedMethod().getName();55 assertEquals("method1",

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