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.matchers; 7 8 import org.junit.Test; 9 import org.mockito.Mock; 10 import org.mockitousage.IMethods; 11 import org.mockitoutil.TestBase; 12 13 import java.util.*; 14 15 import static junit.framework.TestCase.assertEquals; 16 import static junit.framework.TestCase.fail; 17 import static org.mockito.Matchers.*; 18 import static org.mockito.Mockito.verify; 19 import static org.mockito.Mockito.when; 20 21 public class MoreMatchersTest extends TestBase { 22 23 @Mock private IMethods mock; 24 25 @Test should_help_out_with_unnecessary_casting()26 public void should_help_out_with_unnecessary_casting() { 27 when(mock.objectArgMethod(any(String.class))).thenReturn("string"); 28 29 assertEquals("string", mock.objectArgMethod("foo")); 30 } 31 32 @Test any_should_be_actual_alias_to_anyObject()33 public void any_should_be_actual_alias_to_anyObject() { 34 mock.simpleMethod((Object) null); 35 36 verify(mock).simpleMethod(any()); 37 verify(mock).simpleMethod(anyObject()); 38 } 39 40 @Test any_class_should_be_actual_alias_to_isA()41 public void any_class_should_be_actual_alias_to_isA() { 42 mock.simpleMethod(new ArrayList()); 43 44 verify(mock).simpleMethod(isA(List.class)); 45 verify(mock).simpleMethod(any(List.class)); 46 47 48 mock.simpleMethod((String) null); 49 try { 50 verify(mock).simpleMethod(isA(String.class)); 51 fail(); 52 } catch (AssertionError ignored) { } 53 try { 54 verify(mock).simpleMethod(any(String.class)); 55 fail(); 56 } catch (AssertionError ignored) { } 57 } 58 59 @Test should_help_out_with_unnecessary_casting_of_lists()60 public void should_help_out_with_unnecessary_casting_of_lists() { 61 //Below yields compiler warning: 62 //when(mock.listArgMethod(anyList())).thenReturn("list"); 63 when(mock.listArgMethod(anyListOf(String.class))).thenReturn("list"); 64 65 assertEquals("list", mock.listArgMethod(new LinkedList<String>())); 66 assertEquals("list", mock.listArgMethod(Collections.<String>emptyList())); 67 } 68 69 @Test should_help_out_with_unnecessary_casting_of_sets()70 public void should_help_out_with_unnecessary_casting_of_sets() { 71 //Below yields compiler warning: 72 //when(mock.setArgMethod(anySet())).thenReturn("set"); 73 when(mock.setArgMethod(anySetOf(String.class))).thenReturn("set"); 74 75 assertEquals("set", mock.setArgMethod(new HashSet<String>())); 76 assertEquals("set", mock.setArgMethod(Collections.<String>emptySet())); 77 } 78 79 @Test should_help_out_with_unnecessary_casting_of_maps()80 public void should_help_out_with_unnecessary_casting_of_maps() { 81 //Below yields compiler warning: 82 //when(mock.setArgMethod(anySet())).thenReturn("set"); 83 when(mock.forMap(anyMapOf(String.class, String.class))).thenReturn("map"); 84 85 assertEquals("map", mock.forMap(new HashMap<String, String>())); 86 assertEquals("map", mock.forMap(Collections.<String, String>emptyMap())); 87 } 88 89 @Test should_help_out_with_unnecessary_casting_of_collections()90 public void should_help_out_with_unnecessary_casting_of_collections() { 91 //Below yields compiler warning: 92 //when(mock.setArgMethod(anySet())).thenReturn("set"); 93 when(mock.collectionArgMethod(anyCollectionOf(String.class))).thenReturn("collection"); 94 95 assertEquals("collection", mock.collectionArgMethod(new ArrayList<String>())); 96 assertEquals("collection", mock.collectionArgMethod(Collections.<String>emptyList())); 97 } 98 99 @Test should_help_out_with_unnecessary_casting_of_iterables()100 public void should_help_out_with_unnecessary_casting_of_iterables() { 101 //Below yields compiler warning: 102 //when(mock.setArgMethod(anySet())).thenReturn("set"); 103 when(mock.iterableArgMethod(anyIterableOf(String.class))).thenReturn("iterable"); 104 105 assertEquals("iterable", mock.iterableArgMethod(new ArrayList<String>())); 106 assertEquals("iterable", mock.iterableArgMethod(Collections.<String>emptyList())); 107 } 108 109 @Test should_help_out_with_unnecessary_casting_of_nullity_checks()110 public void should_help_out_with_unnecessary_casting_of_nullity_checks() { 111 when(mock.objectArgMethod(isNull(LinkedList.class))).thenReturn("string"); 112 when(mock.objectArgMethod(notNull(LinkedList.class))).thenReturn("string"); 113 when(mock.objectArgMethod(isNotNull(LinkedList.class))).thenReturn("string"); 114 115 assertEquals("string", mock.objectArgMethod(null)); 116 assertEquals("string", mock.objectArgMethod("foo")); 117 assertEquals("string", mock.objectArgMethod("foo")); 118 } 119 120 } 121