dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Relax Mockito.argThat() to accept hamcrest's Matcher<? super T> instead of Matcher<T> #361

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hamcrest MatcherAssert.assertThat() takes a Matcher<? super T>, not a 
Matcher<T>.

This is really convenient when matching on Collections.

Example:

public class MockitoTest {

    interface View {

        void setMessages(List<String> messages);
    }

    @Test
    public void test() {
        View view = Mockito.mock(View.class);
        view.setMessages(Arrays.asList("hello", "world"));
        Mockito.verify(view).setMessages(Mockito.<List<String>> argThat(Matchers.hasSize(2))); // does not compile
        Mockito.verify(view).setMessages(MockitoTest.<List<String>> argThat(Matchers.hasSize(2))); // does compile
    }

    public static <T> T argThat(Matcher<? super T> matcher) {
        return null; // just demonstrate that the test compiles with this method signature.
    }
}

Xavier

Original issue reported on code.google.com by xavier.d...@gmail.com on 1 Aug 2012 at 3:06

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 1 Aug 2012 at 3:24

GoogleCodeExporter commented 8 years ago
Sorry, I had seen issue #297 but thought it was a bit different as it never 
explicitely mentioned Matcher<? super T>.

Original comment by xavier.d...@gmail.com on 1 Aug 2012 at 4:03

GoogleCodeExporter commented 8 years ago
No problems, thx for reporting !

Anyway I don't think, this "defect" will be fixed easily, for the reasons 
mentionned in issue 297, i.e. there might compile error, on older client's code.

The alternative is to create another argThat in your project that with the 
signature of Hamcrest 1.2.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 1 Aug 2012 at 4:31

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 2 Dec 2012 at 10:21