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

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

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.internal;
2
3import java.lang.reflect.Method;
4import java.util.Arrays;
5
6import org.jmock.api.Invocation;
7import org.jmock.api.Invokable;
8
9
10public abstract class FakeObjectMethods implements Invokable {
11    private final Invokable next;
12
13    public FakeObjectMethods(Invokable next) {
14        this.next = next;
15    }
16
17    @Override
18    public String toString() {
19        return next.toString();
20    }
21
22    public Object invoke(Invocation invocation) throws Throwable {
23        Method method = invocation.getInvokedMethod();
24        if (isMethod(method, int.class, "hashCode")) {
25            return fakeHashCode(invocation.getInvokedObject());
26        }
27        else if (isMethod(method, String.class, "toString")) {
28            return fakeToString(invocation.getInvokedObject());
29        }
30        else if (isMethod(method, boolean.class, "equals", Object.class)) {
31            return fakeEquals(invocation.getInvokedObject(), invocation.getParameter(0));
32        }
33        else if (isMethod(method, void.class, "finalize")) {
34            fakeFinalize(invocation.getInvokedObject());
35            return null;
36        }
37        else {
38            return next.invoke(invocation);
39        }
40    }
41
42    protected abstract int fakeHashCode(Object invokedObject);
43
44    protected abstract String fakeToString(Object invokedObject);
45
46    protected abstract boolean fakeEquals(Object invokedObject, Object other);
47
48    protected abstract void fakeFinalize(Object invokedObject);
49
50    private boolean isMethod(Method method, Class<?> returnType, String name, Class<?>... parameterTypes) {
51        return method.getReturnType().equals(returnType)
52            && method.getName().equals(name)
53            && Arrays.equals(method.getParameterTypes(), parameterTypes);
54    }
55
56}
57
Full Screen
copy
1package org.jmock.test.acceptance;
2
3import junit.framework.TestCase;
4
5import org.jmock.Expectations;
6import org.jmock.Mockery;
7import org.jmock.api.ExpectationError;
8
9
10/* Acceptance test for issue JMOCK-30 (http://jira.codehaus.org/browse/JMOCK-30).
11 */
12public class CascadedFailuresAcceptanceTests extends TestCase {
13    public interface MockedType {
14        void realExpectationFailure(int i);
15        void invocationCausedByExpectationFailure();
16        void anotherRealExpectationFailure();
17    }
18
19    private Mockery context = new Mockery();
20    private MockedType mock = context.mock(MockedType.class, "mock");
21    private MockedType otherMock = context.mock(MockedType.class, "otherMock");
22    
23    
24    private void maskedExpectationFailure(MockedType mock1, MockedType mock2) {
25        try {
26            mock1.realExpectationFailure(2);
27        }
28        finally {
29            mock2.invocationCausedByExpectationFailure();
30        }
31    }
32    
33    @Override
34    public void setUp() {
35        context.checking(new Expectations() {{
36            allowing (mock).realExpectationFailure(1);
37        }});
38    }
39    
40    public void testMockeryReportsFirstFailedMethod() {
41        try {
42            maskedExpectationFailure(mock, mock);
43            fail("should have thrown ExpectationError");
44        }
45        catch (ExpectationError e) {
46            assertSame("invoked object",
47                       mock, e.invocation.getInvokedObject());
48            assertEquals("invoked method", 
49                         "realExpectationFailure", e.invocation.getInvokedMethod().getName() );
50        }
51    }
52
53    public void testMockeryReportsFirstFailedObject() {
54        try {
55            maskedExpectationFailure(mock, otherMock);
56            fail("should have thrown ExpectationError");
57        }
58        catch (ExpectationError e) {
59            assertSame("invoked object",
60                       mock, e.invocation.getInvokedObject());
61            assertEquals("invoked method", 
62                         "realExpectationFailure", e.invocation.getInvokedMethod().getName() );
63        }
64    }
65
66    // See issue JMOCK-183 (http://jira.codehaus.org/browse/JMOCK-183).
67    public void testVerifyReportsFirstFailure() {
68        try {
69            mock.realExpectationFailure(2);
70        }
71        catch (ExpectationError e) { /* swallowed */ }
72        
73        try {
74            context.assertIsSatisfied();
75            fail("should have thrown ExpectationError");
76        }
77        catch (ExpectationError e) {
78            assertSame("invoked object",
79                       mock, e.invocation.getInvokedObject());
80            assertEquals("invoked method", 
81                         "realExpectationFailure", e.invocation.getInvokedMethod().getName() );
82        }
83    }
84}
85
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)