rysang / mockito

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

Mocks returning Optional<?> should return Optional.absent() not null #349

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Guava's Optional is described here:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Op
tional.html

It'd be nice when calling a method on a mocked object whose return type is 
Optional<> to return an Optional.absent() instead of null.  

My Mockito skills are such that I don't know if this is possible without 
changes to Mockito; but if it is, let me know.

Original issue reported on code.google.com by jatw...@linuxstuff.org on 19 Jun 2012 at 11:04

GoogleCodeExporter commented 8 years ago
I understand it would be nice, but I don't think we will do it as it will 
require a dependency on Guava.
However you should be able to customize the behavior of the default answer to 
do that.

Mockito.mock(SomeClass.class, DEFAULT_ANSWER_WITH_OPTIONAL);

static Answer DEFAULT_ANSWER_WITH_OPTIONAL = new Answer() {
  // ... your code
}

Would that fit your needs ?

Original comment by brice.du...@gmail.com on 20 Jun 2012 at 8:16

GoogleCodeExporter commented 8 years ago
Interesting. It would be nice if Mockito provided an easy way to register a 
default return value for a certain type. So that you could use the current 
Mockito's defaults plus some more custom stuff.

This is now somewhat possible with the default answer (if you delegate to the 
mockito defaults for all other types) of the MockitoConfiguration.

Original comment by szcze...@gmail.com on 20 Jun 2012 at 11:40

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 20 Jul 2012 at 10:45

GoogleCodeExporter commented 8 years ago
Please see Issue #497 for an enhancement for java.util.Optional.

Original comment by phjar...@gmail.com on 8 Jul 2014 at 8:46