mockito icon indicating copy to clipboard operation
mockito copied to clipboard

Consider using "smart null" answer by default

Open mockitoguy opened this issue 8 years ago • 2 comments

Mockito ships with optional Answer RETURNS_SMART_NULLS that is useful to tackle problems with incorrect / missing stubbing. Do you think returning "smart nulls" should be the default behavior of Mockito mocks? Smart null pointer exception is always better than regular NullPointerException when it comes to debugging tests and figuring out why they fail.

The purpose of this ticket is to get feedback and make a decision whether to make returning "smart null" a default behavior in Mockito 3.

mockitoguy avatar Feb 27 '17 14:02 mockitoguy

+1, I like smart nulls.

Btw, for people wanting to test it right know it was possible to configure it globally for the whole project using MockitoConfiguration even in 1.10.x. I remember that Brice was playing with configuration in 2.x, but MockitoConfiguration seems still to be available.

szpak avatar Feb 27 '17 21:02 szpak

+1000. The question is: why was this not the default from the beginning? Why was Mockito ever released with a different default?

I just wasted more than an hour trying to understand:

  • how can a method whose code never returns null, under any circumstance, could return null, resulting in an impossible-to-happen NullPointerException?
  • why is my debugger not stopping inside this method?

I was lucky that a colleague noticed that the method is on a mock.

Please, please, PLEASE!!! make this the default. For the sanity of your users :)

P.S.: thanks @szpak for pointing a way to make this a global default.

cvmocanu avatar Mar 14 '22 15:03 cvmocanu