Mockito.THROWS_EXCEPTION Answer and @Mock to support default Answer

1,103 views
Skip to first unread message

uklance

unread,
May 25, 2012, 11:29:14 AM5/25/12
to mockito
Hi,
I dislike the way that mockito provides defaults for methods that I
have not mocked and I would prefer mockito to throw a
NotMockedException for an unmocked method invocation. One caveat to
this is that I don't want void methods to throw NotMockedException.
Another issue is that I can't configure a

Currently, I do something like this:

public class MyTest {
   @Test
   public void myTest() {
      List<String> list = Mockito.mock(List.class, new
NotMockedExceptionAnswer());
      list.clear(); // does not throw exception
      list.get(0); // throws NotMockedException
   }

   public static class NotMockedExceptionAnswer implements
Answer<Object> {
      public Object answer(InvocationOnMock invocation) throws
Throwable {
         Method method = invocation.getMethod();
         if (void.class.equals(method.getReturnType())) {
            return null;
         }
         throw new NotMockedException(invocation);
      }
   }
}

I would prefer to do this:

public class MyTest {
   @Mock(Mockito.THROWS_EXCEPTION)
   private List<String> list;

   @Before
   public void before() {
      MockitoAnnotations.initMocks(this);
   }

   @Test
   public void myTest() {
      List<String> list = Mockito.mock(List.class, new
NotMockedExceptionAnswer());
      list.clear(); // does not throw exception
      list.get(0); // throws NotMockedException
   }
}

So, my questions:
1. Mockito currently has some default Answers
(Mockito.RETURN_DEFAULTS, Mockito.RETURNS_SMART_NULLS). Would it be
possible to add Mockito.THROWS_EXCEPTION?

2. Can the @Mock annotation be enhanced to allow me to specify a
default Answer as in my example?

Cheers,
Lance.

Eric Lefevre-Ardant

unread,
May 27, 2012, 3:18:19 AM5/27/12
to moc...@googlegroups.com
Would "verifyZeroInteractions" or "verifyNoMoreInteractions" help?


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


Lance

unread,
May 27, 2012, 7:32:11 AM5/27/12
to moc...@googlegroups.com
Hi Eric, I much prefer the default answer approach to using verify. Verify requires an explicit call in every test method whereas default answers throw exceptions implicitly until the methods are mocked.

Szczepan Faber

unread,
May 27, 2012, 10:12:47 AM5/27/12
to moc...@googlegroups.com
Hey,

New answer onto the Mockito class is not in our plans at the moment :)

Allowing user's Answer type in the annotation is a good idea. At the moment, you can statically configure @Mock with certain answers available in MockitoAnswers. Supporting any type that extends Answer would be much better (even if we lose some compile time safety or we have to instantiate user's Answer). Can you submit an feature request for it?

Cheers!
Szczepan Faber
Principal engineer@gradleware
Lead@mockito

Wesley Lira

unread,
May 30, 2012, 5:49:04 AM5/30/12
to mockito
I was trying to do that, but i couldn't.

https://groups.google.com/group/mockito/browse_thread/thread/c1bd46a93fa285eb/a9bd3d117572c4ed#a9bd3d117572c4ed

On 27 maio, 11:12, Szczepan Faber <szcze...@gmail.com> wrote:
> Hey,
>
> New answer onto the Mockito class is not in our plans at the moment :)
>
> Allowing user's Answer type in the annotation is a good idea. At the
> moment, you can statically configure @Mock with certain answers available
> in MockitoAnswers. Supporting any type that extends Answer would be much
> better (even if we lose some compile time safety or we have to instantiate
> user's Answer). Can you submit an feature request for it?
>
> Cheers!
>
>
>
>
>
>
>
>
>
> On Sun, May 27, 2012 at 1:32 PM, Lance <ukla...@gmail.com> wrote:
> > Hi Eric, I much prefer the default answer approach to using verify. Verify
> > requires an explicit call in every test method whereas default answers
> > throw exceptions implicitly until the methods are mocked.
>
> > On 27 May 2012 08:18, Eric Lefevre-Ardant <e...@ericlefevre.net> wrote:
>
> >> Would "verifyZeroInteractions" or "verifyNoMoreInteractions" help?
>
Reply all
Reply to author
Forward
0 new messages