dhamini-poornachandra / mockito

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

Simpler Answer #298

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi, have you considered adding an easier-to-user Answer like this?

public abstract class EasyAnswer implements Answer<Object> {
    protected InvocationOnMock invocation = null;

    @Override
    public final Object answer(InvocationOnMock invocation) throws Throwable {
        this.invocation = invocation;
        return answer();
    }

    protected abstract Object answer() throws Throwable;

    protected <T> T arg(int index) {
        return (T) invocation.getArguments()[index];
    }
}

which supports this usage

        ThreadFactory threadFactory = mock(ThreadFactory.class);
        when(threadFactory.newThread(any(Runnable.class))).thenAnswer(new EasyAnswer() {
            @Override
            protected Object answer() throws Throwable {
                Runnable r = arg(0);
                return new Thread(r);
            }
        });

Cheers,
Brian

Original issue reported on code.google.com by brianfromoregon on 11 Dec 2011 at 1:06

GoogleCodeExporter commented 8 years ago
Hi,

Not really right now, though this is an interesting idea; "an answer toolkit".

Original comment by brice.du...@gmail.com on 12 Dec 2011 at 9:28

GoogleCodeExporter commented 8 years ago
I have coded something that might help here. It is basically a class in the 
fashion of Answers.class, except that it is dedicated to returning one of the 
parameters.

http://code.google.com/r/ericlef-mockito/source/detail?r=2a33398421f4abc42aa5760
b44ab3881823ac96b

For example, IdentityAnswers.RETURN_FIRST_PARAMETER returns an Answer that can 
return the first parameter passed to the method we are stubbing.
Answers provided are :
    RETURN_FIRST_PARAMETER
    RETURN_SECOND_PARAMETER
    RETURN_LAST_PARAMETER
        RETURN_PARAMETER

Original comment by eric...@gmail.com on 7 Mar 2012 at 9:38

GoogleCodeExporter commented 8 years ago
Personally, I'm that keen on the EasyAnswer abstract class.

I see little benefit of:

arg(0) vs invocation.getArguments()[0]

If I want to solve the use case, I'd rather add a arg(x) method on the 
invocation, so that you can do:

invocation.arg(0)

Adding new object to the public API that is not an interface (it's a class - 
albeit abstract) deserves a better use case I think :)

Regarding the return_x_parameter stuff. We can have it so long it does not 
clutter the 1st level API, that is the Mockito class. We've already found a 
place for such custom answers somewhere just don't remember where :)

Original comment by szcze...@gmail.com on 8 Mar 2012 at 2:52

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 9 Mar 2012 at 11:03

GoogleCodeExporter commented 8 years ago
Issue 281 has been merged into this issue.

Original comment by brice.du...@gmail.com on 9 Mar 2012 at 11:48

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 4 Jul 2012 at 1:00