Repository: mockito/mockito
Issue: Argument matcher anyXxx() (i.e. anyString(), anyList()) should not match nulls
Labels: bug
1.* incompatible

This is a bug I'm seeing in 1.10.8 version (older version has the same issue - tested with 1.9.0).

Given:

``` java
Function<Object, Integer> function = Mockito.mock(Function.class);
when(function.apply(Mockito.anyString())).thenReturn(1);
Integer result = function.apply(2);
```

Expected behavior:

``` java
result == null;
```

Actual behavior:

``` java
result == 1;
```

Note that the function is called with an integer (not a string), and still the mocked function return the value which it should return only when a string is passed. The same works when using anyBoolean() or any other methof from any\* family.


Issue comments:
Let's fix it in Mockito 2.0

I'm currently looking at fixing this.

Just to confirm: the current anyX() behaviour is to just accept anything of any type at all (i.e. anyString()/anyInteger()/etc are all the same as anyObject()), and we'd prefer to instead make all of these methods (except anyObject) actually do type checking, and match only nulls and instances of the specified type. Is that right? I'm basing this on https://code.google.com/p/mockito/issues/detail?id=122, which I hope isn't too out of date.

I think that anyX should not pass for nulls, and should only pass when the object is an instance of given type. Dev team, any thoughts? Might be worth asking on the mailing list what the users think.

We might also change isA matcher to be consistent and not pass for nulls. This would effectively make anyX an alias to isA.

The anyX() should either respect the expectation or not exist at all. Working just like anyObject() make it confusing and also pollute the API with a large number of methods.

I'm with you.

@szczepiq That's actually the current behaviour of isA, it currently does not match nulls: https://github.com/mockito/mockito/blob/master/src/org/mockito/internal/matchers/InstanceOf.java#L24

I think this means we just want to change all of `anyX()` to be an alias to `isA(X.class)`.

anyObject() still seems like an interesting case there though with this. Having it match nulls makes this API confusingly inconsistent, but I think there is still a strong use case for an ultra-general 'give me anything at all' matcher.

We could make anyObject() an alias for `any(Object.class)` (so it doesn't accept nulls), and then add `any()` (simpler) or `anything()` (reads nicer in stub setup calls) matchers? I think those could more reasonably accept nulls without feeling inconsistent.

Obviously this is all not nicely backward compatible, I'm assuming you're fine with that, since you're heading for 2.0. It's definitely something that will catch people out when they migrate though.

I think I wouldn't bother changing anyObject(). It's consistent with the family of anyX() methods: anyInt() anyString(), etc. I like the idea of any() alias to anyObject(). Perhaps we could even deprecate anyObject() in favor of any() but I'd rather keep it separate as it is not a backwards incompat change and can be released any time.

> Obviously this is all not nicely backward compatible

In the changeset, you can include a change to 'version.properties' file that modifes the version to "2.0.0-beta". You'll be the first to code Mockito 2.0 feature! :) We would stay on '-beta' version for some time until we make all backwards incompatible changes. Feel free to help more if you want!

> It's consistent with the family of anyX() methods: anyInt() anyString(), etc.

I don't think it is, is it? With these changes, the below will pass:

``` java
when(mock.stringMethod(anyString())).thenReturn("matched");
when(mock.objectMethod(anyObject())).thenReturn("matched");

assertEquals("matched", mock.stringMethod("string"));
assertEquals(null, mock.stringMethod(null));
// vs:
assertEquals("matched", mock.objectMethod(new Object()));
assertEquals("matched", mock.objectMethod(null)); // inconsistent
```

> Feel free to help more if you want!

Very happy to help out more. Any specific issues you want looking at, or issues that are definitely ready for dev, or should I just dive in?

You can start with this issue :) Let's avoid changing the existing behavior of anyObject() now. We can however think about deprecating it with something like any() (separate discussion).

PR now in. I've left `anyObject()`, `any()` and `any(X.class)` as is for now (matching everything, including nulls), and just fixed `anyX()` to not match nulls (`anyX()` == `isA(X.class)`)

Included in 2.0.0-beta. Thanks for help!

For reference purpose see related work in #510 as well


Pull request review comments:

Patch that resolved the issue:
diff --git a/src/org/mockito/Matchers.java b/src/org/mockito/Matchers.java
index 789d598bb4..a8b71dc79e 100644
--- a/src/org/mockito/Matchers.java
+++ b/src/org/mockito/Matchers.java
@@ -50,13 +50,6 @@
  * This implementation is due static type safety imposed by java compiler.
  * The consequence is that you cannot use <code>anyObject()</code>, <code>eq()</code> methods outside of verified/stubbed method.
  *
- * <p>
- * <b>Warning 2:</b>
- * <p>
- * The any family methods <b>*don't do any type checks*</b>, those are only here to avoid casting
- * in your code. If you want to perform type checks use the {@link #isA(Class)} method.
- * This <b>might</b> however change (type checks could be added) in a future major release.
- *
  * <h1>Custom Argument Matchers</h1>
  * 
  * Use {@link Matchers#argThat} method and pass an instance of hamcrest {@link Matcher}.
@@ -107,133 +100,97 @@ public class Matchers {
     private static final MockingProgress MOCKING_PROGRESS = new ThreadSafeMockingProgress();
 
     /**
-     * Any <code>boolean</code>, <code>Boolean</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>boolean</code> or non-null <code>Boolean</code>
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>false</code>.
      */
     public static boolean anyBoolean() {
-        return reportMatcher(Any.ANY).returnFalse();
+        return reportMatcher(new InstanceOf(Boolean.class)).returnFalse();
     }
 
     /**
-     * Any <code>byte</code>, <code>Byte</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>byte</code> or non-null <code>Byte</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static byte anyByte() {
-        return reportMatcher(Any.ANY).returnZero();
+        return reportMatcher(new InstanceOf(Byte.class)).returnZero();
     }
 
     /**
-     * Any <code>char</code>, <code>Character</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>char</code> or non-null <code>Character</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static char anyChar() {
-        return reportMatcher(Any.ANY).returnChar();
+        return reportMatcher(new InstanceOf(Character.class)).returnChar();
     }
 
     /**
-     * Any int, Integer or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any int or non-null Integer.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static int anyInt() {
-        return reportMatcher(Any.ANY).returnZero();
+        return reportMatcher(new InstanceOf(Integer.class)).returnZero();
     }
 
     /**
-     * Any <code>long</code>, <code>Long</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>long</code> or non-null <code>Long</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static long anyLong() {
-        return reportMatcher(Any.ANY).returnZero();
+        return reportMatcher(new InstanceOf(Long.class)).returnZero();
     }
 
     /**
-     * Any <code>float</code>, <code>Float</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>float</code> or non-null <code>Float</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static float anyFloat() {
-        return reportMatcher(Any.ANY).returnZero();
+        return reportMatcher(new InstanceOf(Float.class)).returnZero();
     }
 
     /**
-     * Any <code>double</code>, <code>Double</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>double</code> or non-null <code>Double</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static double anyDouble() {
-        return reportMatcher(Any.ANY).returnZero();
+        return reportMatcher(new InstanceOf(Double.class)).returnZero();
     }
 
     /**
-     * Any <code>short</code>, <code>Short</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any <code>short</code> or non-null <code>Short</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return <code>0</code>.
      */
     public static short anyShort() {
-        return reportMatcher(Any.ANY).returnZero();
+        return reportMatcher(new InstanceOf(Short.class)).returnZero();
     }
 
     /**
-     * Any <code>Object</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Matches anything, including null.
      * <p>
-     * Has aliases: {@link #any()} and {@link #any(Class clazz)}
+     * This is an alias of: {@link #any()} and {@link #any(java.lang.Class)}
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
@@ -271,20 +228,16 @@ public static <T> T anyVararg() {
     }
     
     /**
-     * Any kind object, not necessary of the given class.
-     * The class argument is provided only to avoid casting.
+     * Matches any object, including nulls
      * <p>
-     * Sometimes looks better than <code>anyObject()</code> - especially when explicit casting is required
+     * This method doesn't do type checks with the given parameter, it is only there
+     * to avoid casting in your code. This might however change (type checks could
+     * be added) in a future major release.
      * <p>
-     * Alias to {@link Matchers#anyObject()}
+     * See examples in javadoc for {@link Matchers} class
      * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * This is an alias of: {@link #any()} and {@link #anyObject()}
      * <p>
-     * See examples in javadoc for {@link Matchers} class
-     * 
-     * @param clazz The type to avoid casting
      * @return <code>null</code>.
      */
     public static <T> T any(Class<T> clazz) {
@@ -292,61 +245,51 @@ public static <T> T any(Class<T> clazz) {
     }
     
     /**
-     * Any object or <code>null</code>.
+     * Matches anything, including nulls
      * <p>
      * Shorter alias to {@link Matchers#anyObject()}
      * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
-     * <p>
      * See examples in javadoc for {@link Matchers} class
-     * 
+     * <p>
+     * This is an alias of: {@link #anyObject()} and {@link #any(java.lang.Class)}
+     * <p>
      * @return <code>null</code>.
      */
     public static <T> T any() {
-        return (T) anyObject();
+        return anyObject();
     }
 
     /**
-     * Any <code>String</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any non-null <code>String</code>
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return empty String ("")
      */
     public static String anyString() {
-        return reportMatcher(Any.ANY).returnString();
+        return reportMatcher(new InstanceOf(String.class)).returnString();
     }
     
     /**
-     * Any <code>List</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any non-null <code>List</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return empty List.
      */
     public static List anyList() {
-        return reportMatcher(Any.ANY).returnList();
-    }    
+        return reportMatcher(new InstanceOf(List.class)).returnList();
+    }
     
     /**
      * Generic friendly alias to {@link Matchers#anyList()}.
      * It's an alternative to &#064;SuppressWarnings("unchecked") to keep code clean of compiler warnings.
      * <p>
-     * Any <code>List</code> or <code>null</code>.
+     * Any non-null <code>List</code>.
      * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * This method doesn't do type checks with the given parameter, it is only there
+     * to avoid casting in your code. This might however change (type checks could
+     * be added) in a future major release.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
@@ -354,33 +297,29 @@ public static List anyList() {
      * @return empty List.
      */
     public static <T> List<T> anyListOf(Class<T> clazz) {
-        return (List) reportMatcher(Any.ANY).returnList();
-    }    
+        return anyList();
+    }
     
     /**
-     * Any <code>Set</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any non-null <code>Set</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      *
      * @return empty Set
      */
     public static Set anySet() {
-        return reportMatcher(Any.ANY).returnSet();
+        return reportMatcher(new InstanceOf(Set.class)).returnSet();
     }
     
     /**
      * Generic friendly alias to {@link Matchers#anySet()}.
      * It's an alternative to &#064;SuppressWarnings("unchecked") to keep code clean of compiler warnings.
      * <p>
-     * Any <code>Set</code> or <code>null</code>
+     * Any non-null <code>Set</code>.
      * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * This method doesn't do type checks with the given parameter, it is only there
+     * to avoid casting in your code. This might however change (type checks could
+     * be added) in a future major release.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      *
@@ -388,33 +327,29 @@ public static Set anySet() {
      * @return empty Set
      */
     public static <T> Set<T> anySetOf(Class<T> clazz) {
-        return (Set) reportMatcher(Any.ANY).returnSet();
+        return anySet();
     }
 
     /**
-     * Any <code>Map</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any non-null <code>Map</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return empty Map.
      */
     public static Map anyMap() {
-        return reportMatcher(Any.ANY).returnMap();
+        return reportMatcher(new InstanceOf(Map.class)).returnMap();
     }
 
     /**
      * Generic friendly alias to {@link Matchers#anyMap()}.
      * It's an alternative to &#064;SuppressWarnings("unchecked") to keep code clean of compiler warnings.
      * <p>
-     * Any <code>Map</code> or <code>null</code>
+     * Any non-null <code>Map</code>.
      * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * This method doesn't do type checks with the given parameter, it is only there
+     * to avoid casting in your code. This might however change (type checks could
+     * be added) in a future major release.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      *
@@ -423,33 +358,29 @@ 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();
     }
     
     /**
-     * Any <code>Collection</code> or <code>null</code>.
-     * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * Any non-null <code>Collection</code>.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
      * @return empty Collection.
      */
     public static Collection anyCollection() {
-        return reportMatcher(Any.ANY).returnList();
+        return reportMatcher(new InstanceOf(Collection.class)).returnList();
     }    
     
     /**
      * Generic friendly alias to {@link Matchers#anyCollection()}.
-     * It's an alternative to &#064;SuppressWarnings("unchecked") to keep code clean of compiler warnings.     
+     * It's an alternative to &#064;SuppressWarnings("unchecked") to keep code clean of compiler warnings.
      * <p>
-     * Any <code>Collection</code> or <code>null</code>.
+     * Any non-null <code>Collection</code>.
      * <p>
-     * This method <b>*don't do any type checks*</b>, it is only there to avoid casting
-     * in your code. This might however change (type checks could be added) in a
-     * future major release.
+     * This method doesn't do type checks with the given parameter, it is only there
+     * to avoid casting in your code. This might however change (type checks could
+     * be added) in a future major release.
      * <p>
      * See examples in javadoc for {@link Matchers} class
      * 
@@ -457,7 +388,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();
     }    
 
     /**
diff --git a/src/org/mockito/internal/matchers/Any.java b/src/org/mockito/internal/matchers/Any.java
index 1876b38cf2..28a98f027a 100644
--- a/src/org/mockito/internal/matchers/Any.java
+++ b/src/org/mockito/internal/matchers/Any.java
@@ -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() {}
     
diff --git a/version.properties b/version.properties
index 0e4d43bdff..c3d41c20f9 100644
--- a/version.properties
+++ b/version.properties
@@ -1,2 +1,2 @@
-version=1.10.17
+version=2.0.0-beta
 mockito.testng.version=1.0
\ No newline at end of file


Test patch:
diff --git a/test/org/mockitousage/IMethods.java b/test/org/mockitousage/IMethods.java
index 557e7c5e14..8c9a1b3359 100644
--- a/test/org/mockitousage/IMethods.java
+++ b/test/org/mockitousage/IMethods.java
@@ -219,4 +219,6 @@ public interface IMethods {
     int toIntPrimitive(Integer i);
 
     Integer toIntWrapper(int i);
+
+    String forObject(Object object);
 }
\ No newline at end of file
diff --git a/test/org/mockitousage/MethodsImpl.java b/test/org/mockitousage/MethodsImpl.java
index 539f612de7..347dfa65f6 100644
--- a/test/org/mockitousage/MethodsImpl.java
+++ b/test/org/mockitousage/MethodsImpl.java
@@ -418,4 +418,8 @@ public int toIntPrimitive(Integer i) {
     public Integer toIntWrapper(int i) {
         return null;
     }
+
+    public String forObject(Object object) {
+        return null;
+    }
 }
diff --git a/test/org/mockitousage/matchers/AnyXMatchersAcceptNullsTest.java b/test/org/mockitousage/matchers/AnyXMatchersAcceptNullsTest.java
index 32c7b6f605..b5d767fe64 100644
--- a/test/org/mockitousage/matchers/AnyXMatchersAcceptNullsTest.java
+++ b/test/org/mockitousage/matchers/AnyXMatchersAcceptNullsTest.java
@@ -24,24 +24,37 @@ public void setUp() {
     }
 
     @Test
-    public void shouldAnyXMatchersAcceptNull() {
-        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");
+    public void shouldAcceptNullsInAnyMatcher() {
+        when(mock.oneArg(any())).thenReturn("matched");
+
+        assertEquals(null, mock.forObject(null));
+    }
+
+    @Test
+    public void shouldAcceptNullsInAnyObjectMatcher() {
+        when(mock.oneArg(anyObject())).thenReturn("matched");
+
+        assertEquals(null, mock.forObject(null));
+    }
+
+    @Test
+    public void shouldNotAcceptNullInAnyXMatchers() {
+        when(mock.oneArg(anyString())).thenReturn("0");
+        when(mock.forList(anyList())).thenReturn("1");
+        when(mock.forMap(anyMap())).thenReturn("2");
+        when(mock.forCollection(anyCollection())).thenReturn("3");
+        when(mock.forSet(anySet())).thenReturn("4");
         
-        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 shouldAnyPrimiteWraperMatchersAcceptNull() {
+    public void shouldNotAcceptNullInAllAnyPrimitiveWrapperMatchers() {
         when(mock.forInteger(anyInt())).thenReturn("0");
         when(mock.forCharacter(anyChar())).thenReturn("1");
         when(mock.forShort(anyShort())).thenReturn("2");
@@ -51,13 +64,13 @@ public void shouldAnyPrimiteWraperMatchersAcceptNull() {
         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));
     }
 }
\ No newline at end of file
diff --git a/test/org/mockitousage/matchers/MatchersTest.java b/test/org/mockitousage/matchers/MatchersTest.java
index 9835bbd660..971a84c84b 100644
--- a/test/org/mockitousage/matchers/MatchersTest.java
+++ b/test/org/mockitousage/matchers/MatchersTest.java
@@ -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");
diff --git a/test/org/mockitousage/matchers/NewMatchersTest.java b/test/org/mockitousage/matchers/NewMatchersTest.java
index 77874f3a60..e9e8f16e13 100644
--- a/test/org/mockitousage/matchers/NewMatchersTest.java
+++ b/test/org/mockitousage/matchers/NewMatchersTest.java
@@ -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());
     }
 }
\ No newline at end of file
diff --git a/test/org/mockitousage/verification/DescriptiveMessagesWhenVerificationFailsTest.java b/test/org/mockitousage/verification/DescriptiveMessagesWhenVerificationFailsTest.java
index 65f6bdbe31..44a074ef5c 100644
--- a/test/org/mockitousage/verification/DescriptiveMessagesWhenVerificationFailsTest.java
+++ b/test/org/mockitousage/verification/DescriptiveMessagesWhenVerificationFailsTest.java
@@ -239,7 +239,10 @@ public void should_print_method_when_matcher_used() throws Exception {
                 "\n" +
                 "Wanted but not invoked:" +
                 "\n" +
-                "iMethods.twoArgumentMethod(<any>, 100);";
+                "iMethods.twoArgumentMethod(\n" +
+                "    isA(java.lang.Integer),\n" +
+                "    100\n" +
+                ");";
             assertContains(expectedMessage, actualMessage);
         }
     }
diff --git a/test/org/mockitousage/verification/PrintingVerboseTypesWithArgumentsTest.java b/test/org/mockitousage/verification/PrintingVerboseTypesWithArgumentsTest.java
index e222c95022..05543dd339 100644
--- a/test/org/mockitousage/verification/PrintingVerboseTypesWithArgumentsTest.java
+++ b/test/org/mockitousage/verification/PrintingVerboseTypesWithArgumentsTest.java
@@ -65,12 +65,12 @@ public void shouldShowTheTypeOfTheMismatchingArgumentWhenOutputDescriptionsForIn
         
         try {
             //when
-            verify(boo).withLongAndInt(eq(100), anyInt());
+            verify(boo).withLongAndInt(eq(100), any(Integer.class));
             fail();
         } catch (ArgumentsAreDifferent e) {
             //then
             assertContains("withLongAndInt((Long) 100, 200)", e.getMessage());
-            assertContains("withLongAndInt((Integer) 100, <any>)", e.getMessage());
+            assertContains("withLongAndInt((Integer) 100, <any>", e.getMessage());
         }
     }