1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.matchers; 6 7 import org.junit.Before; 8 import org.junit.Test; 9 import org.mockito.Mockito; 10 import org.mockitousage.IMethods; 11 import org.mockitoutil.TestBase; 12 13 import java.util.Arrays; 14 import java.util.HashMap; 15 import java.util.HashSet; 16 17 import static org.junit.Assert.assertEquals; 18 import static org.mockito.Mockito.*; 19 20 @SuppressWarnings("unchecked") 21 public class NewMatchersTest extends TestBase { 22 23 private IMethods mock; 24 25 @Before setUp()26 public void setUp() { 27 mock = Mockito.mock(IMethods.class); 28 } 29 30 @Test shouldAllowAnyList()31 public void shouldAllowAnyList() { 32 when(mock.forList(anyListOf(String.class))).thenReturn("matched"); 33 34 assertEquals("matched", mock.forList(Arrays.asList("x", "y"))); 35 assertEquals(null, mock.forList(null)); 36 37 verify(mock, times(1)).forList(anyListOf(String.class)); 38 } 39 40 @Test shouldAllowAnyCollection()41 public void shouldAllowAnyCollection() { 42 when(mock.forCollection(anyCollectionOf(String.class))).thenReturn("matched"); 43 44 assertEquals("matched", mock.forCollection(Arrays.asList("x", "y"))); 45 assertEquals(null, mock.forCollection(null)); 46 47 verify(mock, times(1)).forCollection(anyCollectionOf(String.class)); 48 } 49 50 @Test shouldAllowAnyMap()51 public void shouldAllowAnyMap() { 52 when(mock.forMap(anyMapOf(String.class, String.class))).thenReturn("matched"); 53 54 assertEquals("matched", mock.forMap(new HashMap<String, String>())); 55 assertEquals(null, mock.forMap(null)); 56 57 verify(mock, times(1)).forMap(anyMapOf(String.class, String.class)); 58 } 59 60 @Test shouldAllowAnySet()61 public void shouldAllowAnySet() { 62 when(mock.forSet(anySetOf(String.class))).thenReturn("matched"); 63 64 assertEquals("matched", mock.forSet(new HashSet<String>())); 65 assertEquals(null, mock.forSet(null)); 66 67 verify(mock, times(1)).forSet(anySetOf(String.class)); 68 } 69 70 @Test shouldAllowAnyIterable()71 public void shouldAllowAnyIterable() { 72 when(mock.forIterable(anyIterableOf(String.class))).thenReturn("matched"); 73 74 assertEquals("matched", mock.forIterable(new HashSet<String>())); 75 assertEquals(null, mock.forIterable(null)); 76 77 verify(mock, times(1)).forIterable(anyIterableOf(String.class)); 78 } 79 } 80