cloud1105 / mockito

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

additional AdditionalAnswers #460

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
It would be useful to have a couple more general purpose AdditionalAnswers:

public class ReturnsMethodName implements Answer<String>, Serializable {
   public String answer(InvocationOnMock invocation) throws Throwable {
        return invocation.getMethod().getName();
    }
}

and

public class ReturnsArgumentToStringAt implements Answer<String>, Serializable {
   // ... see details example in ReturnsArgumentAt
   public String answer(InvocationOnMock invocation) throws Throwable {
      // ... validation code
      return invocation.getArguments()[wantedArgumentPosition].toString();
   }
}

A sample use case for the first one is for implementing interfaces that 
represent resources, such as GWT com.google.gwt.i18n.client.Messages 
implementations, where we need quick distinguishable results for each result.

A sample use case for the second new Answer type is any existing use case that 
uses ReturnsArgumentAt but stringifies the result.  I have had several 
instances where both have been useful.

Original issue reported on code.google.com by kandpwel...@gmail.com on 4 Nov 2013 at 9:40

GoogleCodeExporter commented 8 years ago
And of course the corresponding static methods would be great:

public static <String> Answer<String> returnsFirstArgToString();
public static <String> Answer<String> returnsSecondArgToString();
public static <String> Answer<String> returnsLastArgToString();
public static <String> Answer<String> returnsArgToStringAt(int position);

public static <String> Answer<String> returnsMethodName();

Original comment by kandpwel...@gmail.com on 4 Nov 2013 at 9:45

GoogleCodeExporter commented 8 years ago
Interesting, but that may be a codebase  in your test utils. Not sure if the 
community would need that as a whole.

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 1:55

GoogleCodeExporter commented 8 years ago

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