Home | History | Annotate | Download | only in defaultanswers
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 
      6 package org.mockito.internal.stubbing.defaultanswers;
      7 
      8 import org.junit.Assume;
      9 import org.junit.Test;
     10 import org.mockito.invocation.Invocation;
     11 import org.mockitoutil.TestBase;
     12 
     13 import java.util.*;
     14 
     15 import static org.junit.Assert.*;
     16 import static org.mockito.Mockito.mock;
     17 
     18 public 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     @SuppressWarnings("SelfComparison")
     78     @Test
     79     public void should_return_zero_if_mock_is_compared_to_itself() {
     80         //given
     81         Date d = mock(Date.class);
     82         d.compareTo(d);
     83         Invocation compareTo = this.getLastInvocation();
     84 
     85         //when
     86         Object result = values.answer(compareTo);
     87 
     88         //then
     89         assertEquals(0, result);
     90     }
     91 
     92     @Test
     93     public void should_return_empty_Optional() throws Exception {
     94         verify_empty_Optional_is_returned("java.util.stream.Stream", "java.util.Optional");
     95     }
     96 
     97     @Test
     98     public void should_return_empty_OptionalDouble() throws Exception {
     99         verify_empty_Optional_is_returned("java.util.stream.DoubleStream", "java.util.OptionalDouble");
    100     }
    101 
    102     @Test
    103     public void should_return_empty_OptionalInt() throws Exception {
    104         verify_empty_Optional_is_returned("java.util.stream.IntStream", "java.util.OptionalInt");
    105     }
    106 
    107     @Test
    108     public void should_return_empty_OptionalLong() throws Exception {
    109         verify_empty_Optional_is_returned("java.util.stream.LongStream", "java.util.OptionalLong");
    110     }
    111 
    112     private void verify_empty_Optional_is_returned(String streamFqcn, String optionalFqcn) throws Exception {
    113         Class<?> streamType = getClassOrSkipTest(streamFqcn);
    114 
    115         //given
    116         Object stream = mock(streamType);
    117         Object optional = streamType.getMethod("findAny").invoke(stream);
    118         assertNotNull(optional);
    119         assertFalse((Boolean) Class.forName(optionalFqcn).getMethod("isPresent").invoke(optional));
    120 
    121         Invocation findAny = this.getLastInvocation();
    122 
    123         //when
    124         Object result = values.answer(findAny);
    125 
    126         //then
    127         assertEquals(optional, result);
    128     }
    129 
    130     @Test
    131     public void should_return_empty_Stream() throws Exception {
    132         verify_empty_Stream_is_returned("java.util.stream.Stream");
    133     }
    134 
    135     @Test
    136     public void should_return_empty_DoubleStream() throws Exception {
    137         verify_empty_Stream_is_returned("java.util.stream.DoubleStream");
    138     }
    139 
    140     @Test
    141     public void should_return_empty_IntStream() throws Exception {
    142         verify_empty_Stream_is_returned("java.util.stream.IntStream");
    143     }
    144 
    145     @Test
    146     public void should_return_empty_LongStream() throws Exception {
    147         verify_empty_Stream_is_returned("java.util.stream.LongStream");
    148     }
    149 
    150     private void verify_empty_Stream_is_returned(String streamFqcn) throws Exception {
    151         // given
    152         Class<?> streamType = getClassOrSkipTest(streamFqcn);
    153 
    154         // when
    155         Object stream = values.returnValueFor(streamType);
    156         long count = (Long) streamType.getMethod("count").invoke(stream);
    157 
    158         // then
    159         assertEquals("count of empty " + streamFqcn, 0L, count);
    160     }
    161 
    162     /**
    163      * Tries to load the given class. If the class is not found, the complete test is skipped.
    164      */
    165     private Class<?> getClassOrSkipTest(String className) {
    166         try {
    167             return Class.forName(className);
    168         } catch (ClassNotFoundException e) {
    169             Assume.assumeNoException("JVM does not support " + className, e);
    170             return null;
    171         }
    172     }
    173 
    174 }
    175