Skip to content

Instantly share code, notes, and snippets.

@saiday
Created January 27, 2018 16:32
Show Gist options
  • Save saiday/1005a16ef31abedadd2db65301753783 to your computer and use it in GitHub Desktop.
Save saiday/1005a16ef31abedadd2db65301753783 to your computer and use it in GitHub Desktop.
MockitoConfiguration
package org.mockito.configuration;
import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import rx.Observable;
import rx.Single;
/**
* Mockito loads this with reflection, so this class might appear unused.
*/
public class MockitoConfiguration extends DefaultMockitoConfiguration {
/**
* Disables the Mockito cache to prevent Mockito & Robolectric bugs.
*/
public boolean enableClassCache() {
return false;
}
/**
* change default value returned in a method invocation
*/
public Answer<Object> getDefaultAnswer() {
return new ReturnsEmptyValues() {
@Override
public Object answer(InvocationOnMock inv) {
Class<?> type = inv.getMethod().getReturnType();
if (type.isAssignableFrom(Observable.class)) {
return Observable.error(createException(inv));
} else if (type.isAssignableFrom(Single.class)) {
return Single.error(createException(inv));
} else {
return super.answer(inv);
}
}
};
}
private RuntimeException createException(
InvocationOnMock invocation) {
String s = invocation.toString();
return new RuntimeException(
"No mock defined for invocation " + s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment