projoy/mockito

AnyExcept Matchers

GoogleCodeExporter opened this issue · 2 comments

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:

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

  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

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

  • Added labels: Matchers