public final class MockRestAdapter extends Object
Because APIs are defined as interfaces, versions of the API that use mock data can be created by simply implementing the API interface on a class. These mock implementations execute synchronously which is a large deviation from the behavior of those backed by an API call over the network. By wrapping the mock instances using this class, the interface will still use mock data but exhibit the delays and errors that a real network would face.
Create an API interface and a mock implementation of it.
public interface UserService {
@GET("/user/{id}")
User getUser(@Path("id") String userId);
}
public class MockUserService implements UserService {
@Override public User getUser(String userId) {
return new User("Jake");
}
}
Given a RestAdapter an instance of this class can be created by calling from(retrofit.RestAdapter).
MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);Instances of this class should be used as a singleton so that the behavior of every mock service is consistent.
Rather than using the MockUserService directly, pass it through
the create method.
UserService service = mockRestAdapter.create(UserService.class, new MockUserService());The returned
UserService instance will now behave like it is happening over the network
while allowing the mock implementation to be written synchronously.
HTTP errors can be simulated in your mock services by throwing an instance of
MockHttpException. This should be done for both synchronous and asynchronous methods.
Do not call the failure() method of a callback.
| Modifier and Type | Class and Description |
|---|---|
static interface |
MockRestAdapter.ValueChangeListener
A listener invoked when the network behavior values for a
MockRestAdapter change. |
| Modifier and Type | Method and Description |
|---|---|
int |
calculateDelayForCall()
Get the delay (in milliseconds) that should be used for delaying a network call response.
|
int |
calculateDelayForError()
Get the delay (in milliseconds) that should be used for triggering a network error.
|
boolean |
calculateIsFailure()
Randomly determine whether this call should result in a network failure.
|
<T> T |
create(Class<T> service,
T mockService)
Wrap the supplied mock implementation of a service so that it exhibits the delay and error
characteristics of a real network.
|
static MockRestAdapter |
from(RestAdapter restAdapter)
Create a new
MockRestAdapter which will act as a factory for mock services. |
long |
getDelay()
The network round trip delay, in milliseconds
|
int |
getErrorPercentage()
The percentage of calls to
calculateIsFailure() that return true. |
int |
getVariancePercentage()
The plus-or-minus variance percentage of the network round trip delay.
|
void |
setDelay(long delayMs)
Set the network round trip delay, in milliseconds.
|
void |
setErrorPercentage(int errorPct)
Set the percentage of calls to
calculateIsFailure() that return true. |
void |
setValueChangeListener(MockRestAdapter.ValueChangeListener listener)
Set a listener to be notified when any mock value changes.
|
void |
setVariancePercentage(int variancePct)
Set the plus-or-minus variance percentage of the network round trip delay.
|
public static MockRestAdapter from(RestAdapter restAdapter)
MockRestAdapter which will act as a factory for mock services. Some of
the configuration of the supplied RestAdapter will be used generating mock behavior.public void setValueChangeListener(MockRestAdapter.ValueChangeListener listener)
public void setDelay(long delayMs)
public long getDelay()
public void setVariancePercentage(int variancePct)
public int getVariancePercentage()
public void setErrorPercentage(int errorPct)
calculateIsFailure() that return true.public int getErrorPercentage()
calculateIsFailure() that return true.public boolean calculateIsFailure()
This method is exposed for implementing other, non-Retrofit services which exhibit similar
network behavior. Retrofit services automatically will exhibit network behavior when wrapped
using create(Class, Object).
public int calculateDelayForError()
Because we are triggering an error, use a random delay between 0 and three times the normal network delay to simulate a flaky connection failing anywhere from quickly to slowly.
This method is exposed for implementing other, non-Retrofit services which exhibit similar
network behavior. Retrofit services automatically will exhibit network behavior when wrapped
using create(Class, Object).
public int calculateDelayForCall()
This method is exposed for implementing other, non-Retrofit services which exhibit similar
network behavior. Retrofit services automatically will exhibit network behavior when wrapped
using create(Class, Object).
public <T> T create(Class<T> service, T mockService)
Copyright © 2015 Square, Inc.. All Rights Reserved.