joepadde / mockito

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

AnyExcept Matchers #461

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
In addition to the any() matchers it would be nice to specify a vararg for 
values that are not to be included in the matcher.  This is especially nice 
when you wish the stub to throw an exception except for certain values. In my 
case, I was mocking a JNDI lookup service which throws NamingException.

when(mock.lookup(anyStringExcept("Hello"))).thenThrow(new RuntimeException());

This would allow you to then add a stub for "Hello" without it replacing the 
original stub.

when(mock.lookup("Hello")).thenReturn("World!");

Actually, as it is, the previous syntax would cause the RuntimeException to be 
thrown (can only override the stub with the doReturn() clause).

I think the syntax is a very readable improvement over resorting to argThat() 
hamcrest Matchers and it is easy to implement.  I included code that 
accomplishes this.

Here is the basic idea:

    public static String anyStringExcept(String...except) {
        return reportMatcher(new AnyExcept( (Object[])except )).returnString();
    }

Original issue reported on code.google.com by michael....@gmail.com on 7 Nov 2013 at 4:58

Attachments:

GoogleCodeExporter commented 8 years ago
Hi, I think it's not really necessary, there's the AddtionalMatchers class that 
can wrap a mockito matcher.
However =you could maybe extend the AdditionalMatchers to support varargs.

Also w would prefer a GitHub pull request. Thanks for your interest.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 2:01

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 2:01