How to use ReturnDefaultValueActionTests class of org.jmock.test.unit.internal package

Best Jmock-library code snippet using org.jmock.test.unit.internal.ReturnDefaultValueActionTests

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.test.unit.internal;
2
3import org.hamcrest.Matcher;
4import org.jmock.internal.ReturnDefaultValueAction;
5import org.junit.Test;
6
7import javax.xml.ws.handler.LogicalMessageContext;
8import java.beans.beancontext.BeanContext;
9import java.beans.beancontext.BeanContextServices;
10import java.util.*;
11
12import static org.hamcrest.MatcherAssert.assertThat;
13import static org.hamcrest.Matchers.*;
14import static org.jmock.test.unit.internal.ReturnDefaultValueActionTests.invocationReturning;
15
16/**
17 * @author Steve Freeman 2013 http://www.jmock.org
18 *         https://github.com/jmock-developers/jmock-library/issues/9
19 */
20public class ReturnDefaultCollectionTests {
21    abstract static class AbstractCollection implements Collection {}
22    private static final Matcher<Object> IS_PROXY_CLASS = hasProperty("canonicalName", containsString("Proxy"));
23    private final ReturnDefaultValueAction action = new ReturnDefaultValueAction();
24
25    @SuppressWarnings("unchecked")
26    @Test public void
27    returnsANewInstanceForEachCall() throws Throwable {
28        final ArrayList<Object> firstInstance = returnedArrayList();
29        firstInstance.add(new Object());
30
31        assertThat(returnedArrayList(), is(empty()));
32    }
33
34    @Test public void
35    returnsNewInstanceOfIterableClasses() throws Throwable {
36        returnsInstanceForType(ArrayList.class, ArrayList.class);
37        returnsInstanceForType(PriorityQueue.class, PriorityQueue.class);
38    }
39
40    @Test public void
41    returnsNewInstanceOfMapClasses() throws Throwable {
42        returnsInstanceForType(HashMap.class, HashMap.class);
43        returnsInstanceForType(Properties.class, Properties.class);
44    }
45
46    @Test public void
47    returnsNewInstanceConformingToCollectionInterface() throws Throwable {
48        returnsInstanceForType(List.class, LinkedList.class);
49        returnsInstanceForType(Set.class, TreeSet.class);
50        returnsInstanceForType(NavigableSet.class, TreeSet.class);
51        returnsInstanceForType(SortedSet.class, TreeSet.class);
52        returnsInstanceForType(Queue.class, LinkedList.class);
53        returnsInstanceForType(Deque.class, LinkedList.class);
54    }
55
56    // https://github.com/jmock-developers/jmock-library/issues/46
57    @Test public void
58    imposterisesBeanContextTypesToAvoidMissingTypesInAndroid() throws Throwable {
59        assertThat(action.invoke(invocationReturning(BeanContext.class)).getClass(), IS_PROXY_CLASS);
60        assertThat(action.invoke(invocationReturning(BeanContextServices.class)).getClass(), IS_PROXY_CLASS);
61    }
62
63    @Test public void
64    returnsNewInstanceConformingToMapType() throws Throwable {
65        returnsInstanceForType(Map.class, TreeMap.class);
66        returnsInstanceForType(SortedMap.class, TreeMap.class);
67        returnsInstanceForType(NavigableMap.class, TreeMap.class);
68    }
69
70    @Test public void
71    imposterisesUnsupportedMapTypes() throws Throwable {
72        assertThat(action.invoke(invocationReturning(LogicalMessageContext.class)).getClass(), IS_PROXY_CLASS);
73    }
74    
75    @Test public void
76    returnsNullForAbstractCollections() throws Throwable {
77        assertThat(action.invoke(invocationReturning(AbstractCollection.class)), is(nullValue()));
78    }
79
80    private void returnsInstanceForType(Class<?> declaredType, Class<?> expectedType) throws Throwable {
81        assertThat(
82                action.invoke(invocationReturning(declaredType)),
83                instanceOf(expectedType));
84    }
85
86    @SuppressWarnings("unchecked")
87    private ArrayList<Object> returnedArrayList() throws Throwable {
88        return (ArrayList<Object>) action.invoke(invocationReturning(ArrayList.class));
89    }
90
91}
92
Full Screen
copy
1/*  Copyright (c) 2000-2006 jMock.org
2 */
3package org.jmock.test.unit.internal;
4
5import junit.framework.AssertionFailedError;
6import junit.framework.TestCase;
7
8import org.hamcrest.StringDescription;
9import org.jmock.api.Imposteriser;
10import org.jmock.api.Invocation;
11import org.jmock.internal.ReturnDefaultValueAction;
12import org.jmock.lib.JavaReflectionImposteriser;
13import org.jmock.test.unit.support.AssertThat;
14import org.jmock.test.unit.support.MethodFactory;
15
16
17public class ReturnDefaultValueActionTests extends TestCase {
18    static final Object[] NO_ARG_VALUES = new Object[0];
19    static final MethodFactory METHOD_FACTORY = new MethodFactory();
20
21    private ReturnDefaultValueAction action;
22
23    @Override public void setUp() {
24        action = new ReturnDefaultValueAction();
25    }
26
27    public void testWritesDescritionToStringBuffer() {
28        AssertThat.stringIncludes("contains expected description",
29            "returns a default value",
30            StringDescription.toString(action));
31    }
32
33    public void testReturnsUsefulDefaultResultsForBasicTypes()
34        throws Throwable
35    {
36        assertHasRegisteredValue(action, boolean.class, Boolean.FALSE);
37        assertHasRegisteredValue(action, void.class, null);
38        assertHasRegisteredValue(action, byte.class, new Byte((byte)0));
39        assertHasRegisteredValue(action, short.class, new Short((short)0));
40        assertHasRegisteredValue(action, int.class, new Integer(0));
41        assertHasRegisteredValue(action, long.class, new Long(0L));
42        assertHasRegisteredValue(action, char.class, new Character('\0'));
43        assertHasRegisteredValue(action, float.class, new Float(0.0F));
44        assertHasRegisteredValue(action, double.class, new Double(0.0));
45        assertHasRegisteredValue(action, Boolean.class, Boolean.FALSE);
46        assertHasRegisteredValue(action, Byte.class, new Byte((byte)0));
47        assertHasRegisteredValue(action, Short.class, new Short((short)0));
48        assertHasRegisteredValue(action, Integer.class, new Integer(0));
49        assertHasRegisteredValue(action, Long.class, new Long(0L));
50        assertHasRegisteredValue(action, Character.class, new Character('\0'));
51        assertHasRegisteredValue(action, Float.class, new Float(0.0F));
52        assertHasRegisteredValue(action, Double.class, new Double(0.0));
53        assertHasRegisteredValue(action, String.class, "");
54        assertNotNull( "should return an object for Object return type",
55                       action.invoke(invocationReturning(Object.class)) );
56    }
57
58    public void testReturnsEmptyArrayForAllArrayTypes()
59            throws Throwable
60    {
61        int[] defaultArrayForPrimitiveType =
62                (int[])action.invoke(invocationReturning(int[].class));
63        assertEquals("should be empty array", 0, defaultArrayForPrimitiveType.length);
64
65        Object[] defaultArrayForReferenceType =
66                (Object[])action.invoke(invocationReturning(Object[].class));
67        assertEquals("should be empty array", 0, defaultArrayForReferenceType.length);
68    }
69
70    public interface InterfaceType {
71        int returnInt();
72    }
73
74    // Inspired by http://www.c2.com/cgi/wiki?JavaNullProxy
75    public void testIfImposteriserCanImposteriseReturnTypeReturnsNewMockObjectWithSameReturnDefaultValueAction() throws Throwable {
76        Imposteriser imposteriser = new JavaReflectionImposteriser() {
77            @Override
78            public boolean canImposterise(Class<?> c) {
79                return c == InterfaceType.class;
80            }
81        };
82        
83        action = new ReturnDefaultValueAction(imposteriser);
84        
85        int intResult = -1;
86        
87        action.addResult(int.class, new Integer(intResult));
88        
89        InterfaceType result = (InterfaceType)action.invoke(invocationReturning(InterfaceType.class));
90        
91        assertEquals("int result from 'null' interface implementation",
92                     intResult, result.returnInt());
93        
94        assertEquals("should not have returned a mock Runnable because the imposteriser cannot imposterise it",
95                     null, action.invoke(invocationReturning(Runnable.class)));
96    }
97    
98    public void testDefaultResultsCanBeExplicitlyOverriddenByType() throws Throwable {
99        int newDefaultIntResult = 20;
100        String newDefaultStringResult = "hello";
101
102        action.addResult(String.class, newDefaultStringResult);
103        action.addResult(int.class, new Integer(newDefaultIntResult));
104
105        assertEquals("expected registered value for string result type",
106                     newDefaultStringResult, action.invoke(invocationReturning(String.class)));
107
108        assertEquals("expected registered value for int result type",
109                     new Integer(newDefaultIntResult), action.invoke(invocationReturning(int.class)));
110    }
111
112    public void testAnExplicitlyRegisteredResultOverridesThePreviousResultForTheSameType() throws Throwable {
113        action.addResult(String.class, "result1");
114        action.addResult(String.class, "result2");
115
116        assertEquals("expected second result",
117                     "result2", action.invoke(invocationReturning(String.class)));
118    }
119
120    class UnsupportedReturnType
121    {
122    }
123
124    public void testInvocationWithAnUnsupportedReturnTypeReturnsNull()
125        throws Throwable
126    {
127        Class<?> unsupportedReturnType = UnsupportedReturnType.class;
128
129        Object result = action.invoke(invocationReturning(unsupportedReturnType));
130        
131        assertNull("should have returned null", result);
132    }
133    
134    public void assertHasRegisteredValue(ReturnDefaultValueAction action,
135                                         Class<?> resultType,
136                                         Object resultValue )
137            throws Throwable
138    {
139        assertEquals("expected " + resultValue + " to be returned",
140                     resultValue, action.invoke(invocationReturning(resultType)));
141    }
142
143    public void assertHasNotRegisteredReturnType( ReturnDefaultValueAction action,
144                                                  Class<?> resultType )
145            throws Throwable
146    {
147        try {
148            action.invoke(invocationReturning(resultType));
149            fail("action should not support return type " + resultType);
150        }
151        catch (AssertionFailedError expected) {
152            return;
153        }
154    }
155
156    private Invocation invocationReturning(Class<?> resultType) {
157        return new Invocation("INVOKED-OBJECT",
158                              METHOD_FACTORY.newMethodReturning(resultType),
159                              NO_ARG_VALUES);
160    }
161}
162
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

...Most popular Stackoverflow questions on ReturnDefaultValueActionTests

    No relevant questions found for this class 😞

Most used methods in ReturnDefaultValueActionTests

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)