How to use method of org.jmock.test.unit.api.InvocationTests class

Best Jmock-library code snippet using org.jmock.test.unit.api.InvocationTests.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1/*  Copyright (c) 2000-2006 jMock.org
2 */
3package org.jmock.test.unit.api;
4
5import java.lang.reflect.Method;
6import java.util.Arrays;
7
8import junit.framework.TestCase;
9
10import org.jmock.api.Invocation;
11import org.jmock.test.unit.support.AssertThat;
12import org.jmock.test.unit.support.MethodFactory;
13
14
15public class InvocationTests extends TestCase {
16    final Object INVOKED = new Object() {
17        @Override public String toString() { return "INVOKED"; }
18    };
19    final String METHOD_NAME = "methodName";
20    final Class<?>[] ARG_TYPES = new Class[]{int.class, boolean.class};
21    final Class<?> RETURN_TYPE = String.class;
22    final Object[] ARG_VALUES = {new Integer(0), Boolean.TRUE};
23    final Class<?>[] EXCEPTION_TYPES = new Class[]{InterruptedException.class, SecurityException.class};
24
25    MethodFactory methodFactory;
26    Method method;
27
28    public InvocationTests( String name ) {
29        super(name);
30    }
31
32    @Override
33    public void setUp() throws Exception {
34        methodFactory = new MethodFactory();
35        method = methodFactory.newMethod(METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES);
36    }
37
38    public void testCanBeConstructedFromAMethodObject() throws Exception {
39        Invocation invocation = new Invocation(INVOKED, method, ARG_VALUES);
40
41        assertSame("invoked object", INVOKED, invocation.getInvokedObject());
42        assertEquals("invoked method", method, invocation.getInvokedMethod());
43        assertEquals("name", method.getName(), invocation.getInvokedMethod().getName());
44        assertEquals("parameter types",
45                     Arrays.asList(method.getParameterTypes()),
46                     Arrays.asList(invocation.getInvokedMethod().getParameterTypes()));
47        assertEquals("return type",
48                     method.getReturnType(), invocation.getInvokedMethod().getReturnType());
49        assertEquals("parameter count", ARG_VALUES.length, invocation.getParameterCount());
50        assertEquals("parameter values",
51                     Arrays.asList(ARG_VALUES), Arrays.asList(invocation.getParametersAsArray()));
52    }
53
54    public void testConstructorInterpretsNullParameterValueArrayAsZeroArguments() {
55        Invocation invocation = new Invocation(INVOKED, method);
56
57        assertEquals("expected no parameters values",
58                     0, invocation.getParameterCount());
59    }
60
61    public void testTestsForEqualityOnTargetAndMethodSignatureAndArguments() {
62        Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES);
63        Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES);
64        Invocation differentTarget = new Invocation("OTHER TARGET", method, ARG_VALUES);
65
66        Invocation differentMethod = new Invocation(INVOKED,
67                                                    methodFactory.newMethod("OTHER_" + METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES),
68                                                    ARG_VALUES);
69        Invocation differentArgValues = new Invocation(INVOKED, method,
70                                                       new Object[]{new Integer(1), Boolean.FALSE});
71
72        assertTrue("should be equal to itself", invocation1.equals(invocation1));
73        assertTrue("identical calls should be equal", invocation1.equals(invocation2));
74
75        assertFalse("should not be equal to object that is not an Invocation",
76                    invocation1.equals(new Object()));
77        assertFalse("should not be equal to null",
78                    invocation1.equals(null));
79        assertFalse("should not be equal if different invoked object",
80                    invocation1.equals(differentTarget));
81        assertFalse("should not be equal if different method",
82                    invocation1.equals(differentMethod));
83        assertFalse("should not be equal if different argumentValues",
84                    invocation1.equals(differentArgValues));
85    }
86
87    public void testFollowsEqualsHashcodeProtocol() {
88        Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES);
89        Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES);
90
91        assertEquals("should have equal hash codes",
92                     invocation1.hashCode(), invocation2.hashCode());
93    }
94
95    public void testToStringWithTwoArguments() throws Exception {
96        Invocation invocation = new Invocation(INVOKED,
97                                               methodFactory.newMethod(METHOD_NAME, new Class[]{String.class, String.class}, void.class,
98                                                                       EXCEPTION_TYPES),
99                                               new Object[]{"arg1", "arg2"});
100        String result = invocation.toString();
101
102        AssertThat.stringIncludes("Should contain object name", INVOKED.toString(), result);
103        AssertThat.stringIncludes("Should contain method name", METHOD_NAME, result);
104        AssertThat.stringIncludes("Should contain firstArg", "arg1", result);
105        AssertThat.stringIncludes("Should contain second Arg", "arg2", result);
106    }
107
108    public void testToStringWithStringArray() throws Exception {
109        Invocation invocation = new Invocation(INVOKED,
110                                               methodFactory.newMethod(METHOD_NAME, new Class[]{String[].class}, void.class, EXCEPTION_TYPES),
111                                               new Object[]{new String[]{"arg1", "arg2"}});
112        String result = invocation.toString();
113
114        AssertThat.stringIncludes("Should contain method name", METHOD_NAME, result);
115        AssertThat.stringIncludes("Should contain args as an array", "[\"arg1\", \"arg2\"]", result);
116    }
117
118    public void testToStringWithPrimitiveArray() throws Exception {
119        Invocation invocation = new Invocation(INVOKED,
120                                               methodFactory.newMethod(METHOD_NAME, new Class[]{long[].class}, void.class, EXCEPTION_TYPES),
121                                               new Object[]{new long[]{1, 2}});
122        String result = invocation.toString();
123
124        AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result);
125        AssertThat.stringIncludes("Should contain args as an array", "[<1L>, <2L>]", result);
126    }
127
128    public void testMethodToStringWithNullArg() throws Exception {
129        Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class[]{String.class}, void.class, EXCEPTION_TYPES),
130                                               new Object[]{null});
131        String result = invocation.toString();
132
133        AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result);
134        AssertThat.stringIncludes("Should contain firstArg", "null", result);
135    }
136
137    public void testMethodToStringWithObjectArg() throws Exception {
138        final String argAsString = "TO_STRING_RESULT";
139        Object arg = new Object()
140        {
141            @Override public String toString() {
142                return argAsString;
143            }
144        };
145
146        Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class[]{String.class}, void.class, EXCEPTION_TYPES),
147                                               new Object[]{arg});
148        String result = invocation.toString();
149
150        AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result);
151        AssertThat.stringIncludes("Should contain firstArg", argAsString, result);
152    }
153
154    public void testReturnTypeCheckFailsIfReturningValueFromVoidMethod() {
155    	Invocation invocation = 
156            new Invocation(INVOKED, methodFactory.newMethodReturning(void.class));
157        
158        try {
159            invocation.checkReturnTypeCompatibility("string result");
160        }
161        catch (IllegalStateException ex) {
162            AssertThat.stringIncludes("should describe error",
163                                    "tried to return a value from a void method", ex.getMessage());
164            return;
165        }
166        fail("should have failed");
167    }
168    
169    public void testReturnTypeCheckFailsIfReturnedValueIsIncompatible() {
170    	Invocation invocation = 
171            new Invocation(INVOKED, methodFactory.newMethodReturning(int.class));
172
173        try {
174            invocation.checkReturnTypeCompatibility("string result");
175        }
176        catch (IllegalStateException ex) {
177            AssertThat.stringIncludes("expected return type", int.class.toString(), ex.getMessage());
178            AssertThat.stringIncludes("returned value type", String.class.getName(), ex.getMessage());
179            return;
180        }
181        fail("should have failed");
182    }
183
184    public void testReturnTypeCheckFailsWhenReturningNullFromMethodWithPrimitiveReturnType() {
185    	Invocation invocation = 
186            new Invocation(INVOKED, methodFactory.newMethodReturning(int.class));
187    
188        try {
189            invocation.checkReturnTypeCompatibility(null);
190        }
191        catch (IllegalStateException ex) {
192            AssertThat.stringIncludes("expected return type", int.class.toString(), ex.getMessage());
193            AssertThat.stringIncludes("null", String.valueOf((Object)null), ex.getMessage());
194            return;
195        }
196        fail("should have failed");
197    }
198
199    public void testReturnTypeCheckAllowsReturningBoxedTypeFromMethodWithPrimitiveReturnType() {
200        Invocation invocation = 
201            new Invocation(INVOKED, methodFactory.newMethodReturning(int.class));
202    
203        invocation.checkReturnTypeCompatibility(new Integer(0));
204    }
205
206    public void testReturnTypeCheckAllowsReturningNullFromMethodWithNonPrimitiveReturnType() {
207        Invocation invocation = 
208            new Invocation(INVOKED, methodFactory.newMethodReturning(String.class));
209        
210        invocation.checkReturnTypeCompatibility(null);
211    }
212    
213    public void testReturnTypeCheckAllowsReturningNullFromVoidMethod() {
214        Invocation invocation = 
215            new Invocation(INVOKED, methodFactory.newMethodReturning(void.class));
216        
217        invocation.checkReturnTypeCompatibility(null);
218    }
219    
220    public interface TargetInterface {
221        public String doSomething(String arg) throws TargetException;
222    }
223    
224    public static class TargetException extends Exception {}
225    
226    public static class Target implements TargetInterface {
227        public String receivedArg = null;
228        public String result = null;
229        public TargetException exception = null;
230        
231        public String doSomething(String arg) throws TargetException {
232            receivedArg = arg;
233            if (exception != null) {
234                throw exception;
235            }
236            else {
237                return result;
238            }
239        }
240    }
241    
242    public void testCanApplyInvocationToAnotherObject() throws Throwable {
243        Target target = new Target();
244        target.result = "result";
245        
246        Invocation invocation =
247            new Invocation("receiver",
248                           TargetInterface.class.getMethod("doSomething", String.class),
249                           new Object[]{"arg"});
250        
251        String actualResult = (String)invocation.applyTo(target);
252        
253        assertEquals("received argument", "arg", target.receivedArg);
254        assertEquals("result returned from apply", target.result, actualResult);
255    }
256    
257    public void testUnwrapsInvocationTargetExceptionsFromAppliedInvocation() throws Throwable {
258        Target target = new Target();
259        target.exception = new TargetException();
260        
261        Invocation invocation =
262            new Invocation("receiver",
263                           TargetInterface.class.getMethod("doSomething", String.class),
264                           new Object[]{"arg"});
265        
266        try {
267            invocation.applyTo(target);
268            fail("should have thrown TargetException");
269        }
270        catch (TargetException ex) {
271            // expected
272        }
273    }
274}
275
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
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)