ReturnsEmptyValuesTest.java revision 2637d96c202372854a7c71466ddcc6e90fc4fc53
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal.stubbing.defaultanswers;
7
8import org.junit.Assume;
9import org.junit.Test;
10import org.mockito.invocation.Invocation;
11import org.mockitoutil.TestBase;
12
13import java.util.*;
14
15import static junit.framework.TestCase.*;
16import static org.mockito.Mockito.mock;
17
18public class ReturnsEmptyValuesTest extends TestBase {
19
20    private final ReturnsEmptyValues values = new ReturnsEmptyValues();
21
22    @Test
23    public void should_return_empty_collections_or_null_for_non_collections() {
24        assertTrue(((Collection<?>) values.returnValueFor(Collection.class)).isEmpty());
25
26        assertTrue(((Set<?>) values.returnValueFor(Set.class)).isEmpty());
27        assertTrue(((SortedSet<?>) values.returnValueFor(SortedSet.class)).isEmpty());
28        assertTrue(((HashSet<?>) values.returnValueFor(HashSet.class)).isEmpty());
29        assertTrue(((TreeSet<?>) values.returnValueFor(TreeSet.class)).isEmpty());
30        assertTrue(((LinkedHashSet<?>) values.returnValueFor(LinkedHashSet.class)).isEmpty());
31
32        assertTrue(((List<?>) values.returnValueFor(List.class)).isEmpty());
33        assertTrue(((ArrayList<?>) values.returnValueFor(ArrayList.class)).isEmpty());
34        assertTrue(((LinkedList<?>) values.returnValueFor(LinkedList.class)).isEmpty());
35
36        assertTrue(((Map<?, ?>) values.returnValueFor(Map.class)).isEmpty());
37        assertTrue(((SortedMap<?, ?>) values.returnValueFor(SortedMap.class)).isEmpty());
38        assertTrue(((HashMap<?, ?>) values.returnValueFor(HashMap.class)).isEmpty());
39        assertTrue(((TreeMap<?, ?>) values.returnValueFor(TreeMap.class)).isEmpty());
40        assertTrue(((LinkedHashMap<?, ?>) values.returnValueFor(LinkedHashMap.class)).isEmpty());
41
42        assertNull(values.returnValueFor(String.class));
43    }
44
45    @Test
46    public void should_return_empty_iterable() throws Exception {
47        assertFalse(((Iterable<?>) values.returnValueFor(Iterable.class)).iterator().hasNext());
48    }
49
50    @Test
51    public void should_return_primitive() {
52        assertEquals(false, values.returnValueFor(Boolean.TYPE));
53        assertEquals((char) 0, values.returnValueFor(Character.TYPE));
54        assertEquals((byte) 0, values.returnValueFor(Byte.TYPE));
55        assertEquals((short) 0, values.returnValueFor(Short.TYPE));
56        assertEquals(0, values.returnValueFor(Integer.TYPE));
57        assertEquals(0L, values.returnValueFor(Long.TYPE));
58        assertEquals(0F, values.returnValueFor(Float.TYPE));
59        assertEquals(0D, values.returnValueFor(Double.TYPE));
60    }
61
62    @Test
63    public void should_return_non_zero_for_compareTo_method() {
64        //
65        // given
66        Date d = mock(Date.class);
67        d.compareTo(new Date());
68        Invocation compareTo = this.getLastInvocation();
69
70        //when
71        Object result = values.answer(compareTo);
72
73        //then
74        assertTrue(result != (Object) 0);
75    }
76
77    @Test
78    public void should_return_zero_if_mock_is_compared_to_itself() {
79        //given
80        Date d = mock(Date.class);
81        d.compareTo(d);
82        Invocation compareTo = this.getLastInvocation();
83
84        //when
85        Object result = values.answer(compareTo);
86
87        //then
88        assertEquals(0, result);
89    }
90
91    @Test
92    public void should_return_empty_Optional() throws Exception {
93        verify_empty_Optional_is_returned("java.util.stream.Stream", "java.util.Optional");
94    }
95
96    @Test
97    public void should_return_empty_OptionalDouble() throws Exception {
98        verify_empty_Optional_is_returned("java.util.stream.DoubleStream", "java.util.OptionalDouble");
99    }
100
101    @Test
102    public void should_return_empty_OptionalInt() throws Exception {
103        verify_empty_Optional_is_returned("java.util.stream.IntStream", "java.util.OptionalInt");
104    }
105
106    @Test
107    public void should_return_empty_OptionalLong() throws Exception {
108        verify_empty_Optional_is_returned("java.util.stream.LongStream", "java.util.OptionalLong");
109    }
110
111    private void verify_empty_Optional_is_returned(String streamFqcn, String optionalFqcn) throws Exception {
112        Class<?> streamType = getClassOrSkipTest(streamFqcn);
113
114        //given
115        Object stream = mock(streamType);
116        Object optional = streamType.getMethod("findAny").invoke(stream);
117        assertNotNull(optional);
118        assertFalse((Boolean) Class.forName(optionalFqcn).getMethod("isPresent").invoke(optional));
119
120        Invocation findAny = this.getLastInvocation();
121
122        //when
123        Object result = values.answer(findAny);
124
125        //then
126        assertEquals(optional, result);
127    }
128
129    @Test
130    public void should_return_empty_Stream() throws Exception {
131        verify_empty_Stream_is_returned("java.util.stream.Stream");
132    }
133
134    @Test
135    public void should_return_empty_DoubleStream() throws Exception {
136        verify_empty_Stream_is_returned("java.util.stream.DoubleStream");
137    }
138
139    @Test
140    public void should_return_empty_IntStream() throws Exception {
141        verify_empty_Stream_is_returned("java.util.stream.IntStream");
142    }
143
144    @Test
145    public void should_return_empty_LongStream() throws Exception {
146        verify_empty_Stream_is_returned("java.util.stream.LongStream");
147    }
148
149    private void verify_empty_Stream_is_returned(String streamFqcn) throws Exception {
150        // given
151        Class<?> streamType = getClassOrSkipTest(streamFqcn);
152
153        // when
154        Object stream = values.returnValueFor(streamType);
155        long count = (Long) streamType.getMethod("count").invoke(stream);
156
157        // then
158        assertEquals("count of empty " + streamFqcn, 0L, count);
159    }
160
161    /**
162     * Tries to load the given class. If the class is not found, the complete test is skipped.
163     */
164    private Class<?> getClassOrSkipTest(String className) {
165        try {
166            return Class.forName(className);
167        } catch (ClassNotFoundException e) {
168            Assume.assumeNoException("JVM does not support " + className, e);
169            return null;
170        }
171    }
172
173}
174