Permalink
Browse files

Stop anyX() methods matching null inputs

Fixes #134
  • Loading branch information...
pimterry authored and mockitoguy committed Dec 15, 2014
1 parent a7ba606 commit dc205824dbc289acbcde919e430176ad72da847f
@@ -5,6 +5,7 @@
package org.mockito;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsNull;
import org.mockito.internal.matchers.*;
import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
import org.mockito.internal.progress.HandyReturnValues;
@@ -118,7 +119,7 @@
* @return <code>false</code>.
*/
public static boolean anyBoolean() {
return reportMatcher(Any.ANY).returnFalse();
return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
}
/**
@@ -133,7 +134,7 @@ public static boolean anyBoolean() {
* @return <code>0</code>.
*/
public static byte anyByte() {
return reportMatcher(Any.ANY).returnZero();
return reportMatcher(new InstanceOf(Byte.class)).returnZero();
}
/**
@@ -148,7 +149,7 @@ public static byte anyByte() {
* @return <code>0</code>.
*/
public static char anyChar() {
return reportMatcher(Any.ANY).returnChar();
return reportMatcher(new InstanceOf(Character.class)).returnChar();
}
/**
@@ -163,7 +164,7 @@ public static char anyChar() {
* @return <code>0</code>.
*/
public static int anyInt() {
return reportMatcher(Any.ANY).returnZero();
return reportMatcher(new InstanceOf(Integer.class)).returnZero();
}
/**
@@ -178,7 +179,7 @@ public static int anyInt() {
* @return <code>0</code>.
*/
public static long anyLong() {
return reportMatcher(Any.ANY).returnZero();
return reportMatcher(new InstanceOf(Long.class)).returnZero();
}
/**
@@ -193,7 +194,7 @@ public static long anyLong() {
* @return <code>0</code>.
*/
public static float anyFloat() {
return reportMatcher(Any.ANY).returnZero();
return reportMatcher(new InstanceOf(Float.class)).returnZero();
}
/**
@@ -208,7 +209,7 @@ public static float anyFloat() {
* @return <code>0</code>.
*/
public static double anyDouble() {
return reportMatcher(Any.ANY).returnZero();
return reportMatcher(new InstanceOf(Double.class)).returnZero();
}
/**
@@ -223,7 +224,7 @@ public static double anyDouble() {
* @return <code>0</code>.
*/
public static short anyShort() {
return reportMatcher(Any.ANY).returnZero();
return reportMatcher(new InstanceOf(Short.class)).returnZero();
}
/**
@@ -240,7 +241,7 @@ public static short anyShort() {
* @return <code>null</code>.
*/
public static <T> T anyObject() {
return (T) reportMatcher(Any.ANY).returnNull();
return (T) reportMatcher(new InstanceOf(Object.class)).returnNull();
}
/**
@@ -288,7 +289,7 @@ public static short anyShort() {
* @return <code>null</code>.
*/
public static <T> T any(Class<T> clazz) {
return (T) reportMatcher(Any.ANY).returnFor(clazz);
return (T) reportMatcher(new InstanceOf(clazz)).returnFor(clazz);
}
/**
@@ -305,7 +306,7 @@ public static short anyShort() {
* @return <code>null</code>.
*/
public static <T> T any() {
return (T) anyObject();
return (T) reportMatcher(Any.ANY).returnNull();
}
/**
@@ -320,7 +321,7 @@ public static short anyShort() {
* @return empty String ("")
*/
public static String anyString() {
return reportMatcher(Any.ANY).returnString();
return reportMatcher(new InstanceOf(String.class)).returnString();
}
/**
@@ -335,7 +336,7 @@ public static String anyString() {
* @return empty List.
*/
public static List anyList() {
return reportMatcher(Any.ANY).returnList();
return reportMatcher(new InstanceOf(List.class)).returnList();
}
/**
@@ -354,7 +355,7 @@ public static List anyList() {
* @return empty List.
*/
public static <T> List<T> anyListOf(Class<T> clazz) {
return (List) reportMatcher(Any.ANY).returnList();
return anyList();
}
/**
@@ -369,7 +370,7 @@ public static List anyList() {
* @return empty Set
*/
public static Set anySet() {
return reportMatcher(Any.ANY).returnSet();
return reportMatcher(new InstanceOf(Set.class)).returnSet();
}
/**
@@ -388,7 +389,7 @@ public static Set anySet() {
* @return empty Set
*/
public static <T> Set<T> anySetOf(Class<T> clazz) {
return (Set) reportMatcher(Any.ANY).returnSet();
return anySet();
}
/**
@@ -403,7 +404,7 @@ public static Set anySet() {
* @return empty Map.
*/
public static Map anyMap() {
return reportMatcher(Any.ANY).returnMap();
return reportMatcher(new InstanceOf(Map.class)).returnMap();
}
/**
@@ -423,7 +424,7 @@ public static Map anyMap() {
* @return empty Map.
*/
public static <K, V> Map<K, V> anyMapOf(Class<K> keyClazz, Class<V> valueClazz) {
return reportMatcher(Any.ANY).returnMap();
return anyMap();
}
/**
@@ -438,7 +439,7 @@ public static Map anyMap() {
* @return empty Collection.
*/
public static Collection anyCollection() {
return reportMatcher(Any.ANY).returnList();
return reportMatcher(new InstanceOf(Collection.class)).returnList();
}
/**
@@ -457,7 +458,7 @@ public static Collection anyCollection() {
* @return empty Collection.
*/
public static <T> Collection<T> anyCollectionOf(Class<T> clazz) {
return (Collection) reportMatcher(Any.ANY).returnList();
return anyCollection();
}
/**
@@ -14,7 +14,7 @@
public class Any extends ArgumentMatcher implements Serializable {
private static final long serialVersionUID = -4062420125651019029L;
public static final Any ANY = new Any();
public static final Any ANY = new Any();
private Any() {}
@@ -219,4 +219,6 @@
int toIntPrimitive(Integer i);
Integer toIntWrapper(int i);
String forObject(Object object);
}
@@ -418,4 +418,8 @@ public int toIntPrimitive(Integer i) {
public Integer toIntWrapper(int i) {
return null;
}
public String forObject(Object object) {
return null;
}
}
@@ -24,24 +24,24 @@ public void setUp() {
}
@Test
public void shouldAnyXMatchersAcceptNull() {
public void shouldNotAcceptNullInAnyXMatchers() {
when(mock.oneArg(anyObject())).thenReturn("0");
when(mock.oneArg(anyString())).thenReturn("1");
when(mock.forList(anyList())).thenReturn("2");
when(mock.forMap(anyMap())).thenReturn("3");
when(mock.forCollection(anyCollection())).thenReturn("4");
when(mock.forSet(anySet())).thenReturn("5");
assertEquals("0", mock.oneArg((Object) null));
assertEquals("1", mock.oneArg((String) null));
assertEquals("2", mock.forList(null));
assertEquals("3", mock.forMap(null));
assertEquals("4", mock.forCollection(null));
assertEquals("5", mock.forSet(null));
assertEquals(null, mock.oneArg((Object) null));
assertEquals(null, mock.oneArg((String) null));
assertEquals(null, mock.forList(null));
assertEquals(null, mock.forMap(null));
assertEquals(null, mock.forCollection(null));
assertEquals(null, mock.forSet(null));
}
@Test
public void shouldAcceptNullsInAllPrimitiveWrapperAnyMatchers() {
public void shouldNotAcceptNullInAllAnyPrimitiveWrapperMatchers() {
when(mock.forInteger(anyInt())).thenReturn("0");
when(mock.forCharacter(anyChar())).thenReturn("1");
when(mock.forShort(anyShort())).thenReturn("2");
@@ -51,13 +51,13 @@ public void shouldAcceptNullsInAllPrimitiveWrapperAnyMatchers() {
when(mock.forFloat(anyFloat())).thenReturn("6");
when(mock.forDouble(anyDouble())).thenReturn("7");
assertEquals("0", mock.forInteger(null));
assertEquals("1", mock.forCharacter(null));
assertEquals("2", mock.forShort(null));
assertEquals("3", mock.forByte(null));
assertEquals("4", mock.forBoolean(null));
assertEquals("5", mock.forLong(null));
assertEquals("6", mock.forFloat(null));
assertEquals("7", mock.forDouble(null));
assertEquals(null, mock.forInteger(null));
assertEquals(null, mock.forCharacter(null));
assertEquals(null, mock.forShort(null));
assertEquals(null, mock.forByte(null));
assertEquals(null, mock.forBoolean(null));
assertEquals(null, mock.forLong(null));
assertEquals(null, mock.forFloat(null));
assertEquals(null, mock.forDouble(null));
}
}
@@ -225,15 +225,25 @@ public void compareToMatcher() {
@Test
public void anyStringMatcher() {
when(mock.oneArg(anyString())).thenReturn("1");
when(mock.oneArg(anyString())).thenReturn("matched");
assertEquals("1", mock.oneArg(""));
assertEquals("1", mock.oneArg("any string"));
assertEquals(null, mock.oneArg((Object) null));
assertEquals("matched", mock.oneArg(""));
assertEquals("matched", mock.oneArg("any string"));
assertEquals(null, mock.oneArg((String) null));
}
@Test
public void anyMatcher() {
when(mock.forObject(any())).thenReturn("matched");
assertEquals("matched", mock.forObject(123));
assertEquals("matched", mock.forObject("any string"));
assertEquals("matched", mock.forObject("any string"));
assertEquals("matched", mock.forObject((Object) null));
}
@Test
public void anyXMatcher() {
when(mock.oneArg(anyBoolean())).thenReturn("0");
when(mock.oneArg(anyByte())).thenReturn("1");
when(mock.oneArg(anyChar())).thenReturn("2");
@@ -29,41 +29,41 @@ public void setUp() {
@Test
public void shouldAllowAnyList() {
when(mock.forList(anyList())).thenReturn("x");
when(mock.forList(anyList())).thenReturn("matched");
assertEquals("x", mock.forList(null));
assertEquals("x", mock.forList(Arrays.asList("x", "y")));
verify(mock, times(2)).forList(anyList());
assertEquals("matched", mock.forList(Arrays.asList("x", "y")));
assertEquals(null, mock.forList(null));
verify(mock, times(1)).forList(anyList());
}
@Test
public void shouldAllowAnyCollection() {
when(mock.forCollection(anyCollection())).thenReturn("x");
when(mock.forCollection(anyCollection())).thenReturn("matched");
assertEquals("x", mock.forCollection(null));
assertEquals("x", mock.forCollection(Arrays.asList("x", "y")));
verify(mock, times(2)).forCollection(anyCollection());
assertEquals("matched", mock.forCollection(Arrays.asList("x", "y")));
assertEquals(null, mock.forCollection(null));
verify(mock, times(1)).forCollection(anyCollection());
}
@Test
public void shouldAllowAnyMap() {
when(mock.forMap(anyMap())).thenReturn("x");
when(mock.forMap(anyMap())).thenReturn("matched");
assertEquals("x", mock.forMap(null));
assertEquals("x", mock.forMap(new HashMap<String, String>()));
verify(mock, times(2)).forMap(anyMap());
assertEquals("matched", mock.forMap(new HashMap<String, String>()));
assertEquals(null, mock.forMap(null));
verify(mock, times(1)).forMap(anyMap());
}
@Test
public void shouldAllowAnySet() {
when(mock.forSet(anySet())).thenReturn("x");
when(mock.forSet(anySet())).thenReturn("matched");
assertEquals("x", mock.forSet(null));
assertEquals("x", mock.forSet(new HashSet<String>()));
verify(mock, times(2)).forSet(anySet());
assertEquals("matched", mock.forSet(new HashSet<String>()));
assertEquals(null, mock.forSet(null));
verify(mock, times(1)).forSet(anySet());
}
}

0 comments on commit dc20582

Please sign in to comment.