joepadde / mockito

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

FR: Answers.RETURNS_THIS #455

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Sometimes I find myself mocking builder-like interfaces where all-but-one 
methods return the builder, and one method returns the actual result.  I can do 
it this way:

  final Foo result = ...;
  FooBuilderInterface builder =
    Mockito.mock(FooBuilderInterface.class, new Answer<Object>() {
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        if ("build".equals(invocation.getMethod().getName())) {
          return result;
        }
        return invocation.getMock();
      }
    })

But I would really like to do:
  @Mock(answer = Answers.RETURNS_THIS) FooBuilderInterface builder;

    Foo result = ...;
    Mockito.when(builder.build()).thenReturn(result);

Original issue reported on code.google.com by andrewba...@google.com on 11 Oct 2013 at 5:38

GoogleCodeExporter commented 8 years ago
I think this is a reasonable idea, I see much more Joshua Bloch style builders 
nowaday.

Is it possible to make a pull request on github

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

GoogleCodeExporter commented 8 years ago
How exactly you would like the RETURN_THIS to work? E.g. do you want to include 
this conditional check for 'build' method?

Thanks for report!

Original comment by szcze...@gmail.com on 19 Jan 2014 at 9:25

GoogleCodeExporter commented 8 years ago
Return this from all unmocked methods.  No particular special case for
"build".

Original comment by andrewba...@google.com on 20 Jan 2014 at 6:01

GoogleCodeExporter commented 8 years ago
Ok, then I'm ok with this feature.

Original comment by szcze...@gmail.com on 20 Jan 2014 at 6:02

GoogleCodeExporter commented 8 years ago
Agreed no special cases for build. Just return the default answer I believe, 
i.e in most cases null if not stubbed.

Original comment by brice.du...@gmail.com on 20 Jan 2014 at 6:04