question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default answer can be overridden by doReturn(..), but not when(..)

See original GitHub issue

I just discovered that mocks with default Answers can not have their behavior overridden using stubbing with when(..), only with doReturn(..). The javadoc for Mockito.mock(Class, Answer) says that “It is the default answer so it will be used only when you don’t stub the method call.”, but this seems to only be the case if I stub method calls using doReturn (or any doXyz-method I suppose), but not when stubbing with when(..).

The following code demonstrates this:

abstract class MyInterface {
    abstract byte[] getSomeData();
}
MyInterface mock = mock(MyInterface.class, i -> {
    throw new UnsupportedOperationException("method: " + i.getMethod());
});
when(mock.getSomeData()).thenReturn(new byte[0]);
//doReturn(new byte[0]).when(mock).getSomeData();

mock.getSomeData();

(Can be pasted into any test method. I used an abstract class to have the code self-contained for a method scope, but the behavior is the same for an interface.)

The invocation of mock.getSomeData() will invoke the default answer and throw the UnsupportedOperationException. If you comment out the when-stubbing, and uncomment the doReturn-stubbing, the mock.getSomeData() yields the expected stubbed return value.

I have tested this with v2.10.0 and v2.18.3, and I observe the same behavior.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TimvdLippecommented, May 4, 2018

Not an embarrassment at all! We have had multiple of these reports and we are looking for alternatives to resolve this situation. Good luck mocking 😃

0reactions
laymaincommented, Jul 29, 2021

Being able to set te default answer after stubbing could be a solution, what do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I override default Answers on a Mockito mock?
The answer is to use the "doVerb" methods, such as doAnswer , doReturn , and doThrow , which I like to call "Yoda...
Read more >
Mockito 2.2.9 API - javadoc.io
It is the default answer so it will be used only when you don't stub the method ... method does not have an...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Default Return Values. Calling mock(PasswordEncoder.class) returns an instance of PasswordEncoder . We can even call its methods, but what will they ...
Read more >
Mockito: doReturn vs thenReturn - Sangsoo Nam
The parameter of doReturn is Object unlike thenReturn . So, there is no type checking in the compile time. When the type is...
Read more >
Mockito gotcha: Beware of method invocation when stubbing
The methodCall value, which is the return value from the invocation, is simply not used. In our case, it would be null, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found