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