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.mockitousage.stubbing; 7 8 import org.junit.Test; 9 import org.mockito.Mock; 10 import org.mockito.Mockito; 11 import org.mockitousage.IMethods; 12 import org.mockitoutil.TestBase; 13 14 import java.util.HashSet; 15 import java.util.LinkedList; 16 import java.util.List; 17 import java.util.Map; 18 19 import static junit.framework.TestCase.assertEquals; 20 import static junit.framework.TestCase.assertTrue; 21 22 @SuppressWarnings("unchecked") 23 public class ReturningDefaultValuesTest extends TestBase { 24 25 @Mock private IMethods mock; 26 27 @Test shouldReturnAllKindsOfPrimitives()28 public void shouldReturnAllKindsOfPrimitives() throws Exception { 29 assertEquals((byte) 0, mock.byteReturningMethod()); 30 assertEquals((short) 0, mock.shortReturningMethod()); 31 assertEquals(0, mock.intReturningMethod()); 32 assertEquals(0L, mock.longReturningMethod()); 33 assertEquals(0.0F, mock.floatReturningMethod(), 0.0F); 34 assertEquals(0.0D, mock.doubleReturningMethod(), 0.0D); 35 assertEquals((char) 0, mock.charReturningMethod()); 36 assertEquals(false, mock.booleanReturningMethod()); 37 assertEquals(null, mock.objectReturningMethod()); 38 } 39 40 @Test shouldReturnTheSameValuesForWrapperClasses()41 public void shouldReturnTheSameValuesForWrapperClasses() throws Exception { 42 assertEquals(new Byte((byte) 0), mock.byteObjectReturningMethod()); 43 assertEquals(new Short((short) 0), mock.shortObjectReturningMethod()); 44 assertEquals(new Integer(0), mock.integerReturningMethod()); 45 assertEquals(new Long(0L), mock.longObjectReturningMethod()); 46 assertEquals(new Float(0.0F), mock.floatObjectReturningMethod(), 0.0F); 47 assertEquals(new Double(0.0D), mock.doubleObjectReturningMethod(), 0.0D); 48 assertEquals(new Character((char) 0), mock.charObjectReturningMethod()); 49 assertEquals(new Boolean(false), mock.booleanObjectReturningMethod()); 50 } 51 52 @Test shouldReturnEmptyCollections()53 public void shouldReturnEmptyCollections() { 54 CollectionsServer mock = Mockito.mock(CollectionsServer.class); 55 56 assertTrue(mock.list().isEmpty()); 57 assertTrue(mock.linkedList().isEmpty()); 58 assertTrue(mock.map().isEmpty()); 59 assertTrue(mock.hashSet().isEmpty()); 60 } 61 62 @Test shouldReturnMutableEmptyCollection()63 public void shouldReturnMutableEmptyCollection() { 64 CollectionsServer mock = Mockito.mock(CollectionsServer.class); 65 66 List list = mock.list(); 67 list.add("test"); 68 69 assertTrue(mock.list().isEmpty()); 70 } 71 72 private class CollectionsServer { list()73 List<?> list() { 74 return null; 75 } 76 linkedList()77 LinkedList<?> linkedList() { 78 return null; 79 } 80 map()81 Map<?, ?> map() { 82 return null; 83 } 84 hashSet()85 HashSet<?> hashSet() { 86 return null; 87 } 88 } 89 } 90