Home | History | Annotate | Download | only in matchers
      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 static org.junit.Assert.assertEquals;
     14 import static org.mockito.Matchers.*;
     15 import static org.mockito.Mockito.when;
     16 
     17 @SuppressWarnings("unchecked")
     18 public class AnyXMatchersAcceptNullsTest extends TestBase {
     19 
     20     private IMethods mock;
     21 
     22     @Before
     23     public void setUp() {
     24         mock = Mockito.mock(IMethods.class);
     25     }
     26 
     27     @Test
     28     public void shouldAcceptNullsInAnyMatcher() {
     29         when(mock.oneArg((Object) any())).thenReturn("matched");
     30 
     31         assertEquals(null, mock.forObject(null));
     32     }
     33 
     34     @Test
     35     public void shouldAcceptNullsInAnyObjectMatcher() {
     36         when(mock.oneArg((Object) anyObject())).thenReturn("matched");
     37 
     38         assertEquals(null, mock.forObject(null));
     39     }
     40 
     41     @Test
     42     public void shouldNotAcceptNullInAnyXMatchers() {
     43         when(mock.oneArg(anyString())).thenReturn("0");
     44         when(mock.forList(anyListOf(String.class))).thenReturn("1");
     45         when(mock.forMap(anyMapOf(String.class, String.class))).thenReturn("2");
     46         when(mock.forCollection(anyCollectionOf(String.class))).thenReturn("3");
     47         when(mock.forSet(anySetOf(String.class))).thenReturn("4");
     48 
     49         assertEquals(null, mock.oneArg((Object) null));
     50         assertEquals(null, mock.oneArg((String) null));
     51         assertEquals(null, mock.forList(null));
     52         assertEquals(null, mock.forMap(null));
     53         assertEquals(null, mock.forCollection(null));
     54         assertEquals(null, mock.forSet(null));
     55     }
     56 
     57     @Test
     58     public void shouldNotAcceptNullInAllAnyPrimitiveWrapperMatchers() {
     59         when(mock.forInteger(anyInt())).thenReturn("0");
     60         when(mock.forCharacter(anyChar())).thenReturn("1");
     61         when(mock.forShort(anyShort())).thenReturn("2");
     62         when(mock.forByte(anyByte())).thenReturn("3");
     63         when(mock.forBoolean(anyBoolean())).thenReturn("4");
     64         when(mock.forLong(anyLong())).thenReturn("5");
     65         when(mock.forFloat(anyFloat())).thenReturn("6");
     66         when(mock.forDouble(anyDouble())).thenReturn("7");
     67 
     68         assertEquals(null, mock.forInteger(null));
     69         assertEquals(null, mock.forCharacter(null));
     70         assertEquals(null, mock.forShort(null));
     71         assertEquals(null, mock.forByte(null));
     72         assertEquals(null, mock.forBoolean(null));
     73         assertEquals(null, mock.forLong(null));
     74         assertEquals(null, mock.forFloat(null));
     75         assertEquals(null, mock.forDouble(null));
     76     }
     77 }
     78