Skip to content

Instantly share code, notes, and snippets.

@niftynei
Created June 17, 2015 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftynei/589ae5a00b8343b84b41 to your computer and use it in GitHub Desktop.
Save niftynei/589ae5a00b8343b84b41 to your computer and use it in GitHub Desktop.
fun with Answers
public static final String LESS_THAN_A_MINUTE = "less than a minute";
public static final String SECONDS = "seconds";
public static final String MINUTES = "minutes";
public static final String HOURS = "hours";
public static final String DAYS = "days";
public static final String WEEKS = "weeks";
public static final String MONTHS = "months";
public static final String YEARS = "years";
public static final String DECADES = "decades";
public static final String CENTURIES = "centuries";
public static final String MILLENNIA = "millennia";
public static final String SPACE = " ";
public static final String COMMA = ",";
@Before
public void init() {
MockitoAnnotations.initMocks(this);
when(mResourcesMock.getString(R.string.less_than_a_minute)).thenReturn(LESS_THAN_A_MINUTE);
when(mResourcesMock.getQuantityString(eq(R.plurals.seconds), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(SECONDS));
when(mResourcesMock.getQuantityString(eq(R.plurals.minute), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(MINUTES));
when(mResourcesMock.getQuantityString(eq(R.plurals.hour), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(HOURS));
when(mResourcesMock.getQuantityString(eq(R.plurals.day), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(DAYS));
when(mResourcesMock.getQuantityString(eq(R.plurals.week), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(WEEKS));
when(mResourcesMock.getQuantityString(eq(R.plurals.month), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(MONTHS));
when(mResourcesMock.getQuantityString(eq(R.plurals.year), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(YEARS));
when(mResourcesMock.getQuantityString(eq(R.plurals.decade), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(DECADES));
when(mResourcesMock.getQuantityString(eq(R.plurals.century), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(CENTURIES));
when(mResourcesMock.getQuantityString(eq(R.plurals.millennia), any(Integer.class), any(Integer.class))).thenAnswer(new TimeAnswer(MILLENNIA));
timeAgo = new TimeAgo(mResourcesMock);
}
/**
* Class to mimic string replacement of the getQuantity method for resources
*/
private static class TimeAnswer implements Answer<String> {
String mReturnType;
public TimeAnswer(String returnType) {
mReturnType = returnType;
}
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
int value = (int) args[2];
return value + " " + mReturnType;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment