问题描述
我们使用Retrofit 1模拟Web服务并模拟网络延迟,如下所示:
Using Retrofit 1 we used to mock web services and simulate network latency as following:
MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
return mockRestAdapter.create(MyService.class, new MyServiceMock());
其中 MyService
是服务接口(将响应作为Rx Observables返回),而 MyServiceMock
是实现此接口的类.
Where MyService
is the service Interface (returning responses as Rx Observables), and MyServiceMock
is a class that implements this interface.
在Retrofit 2.0.0-beta3中,有一个全新的模拟系统(请参阅: https://github.com/square/retrofit/pull/1343 ),但尚未记录.当尝试类似的东西时,我得到:
In Retrofit 2.0.0-beta3 there is a brand new mocking system (see: https://github.com/square/retrofit/pull/1343) that is not yet documented.When trying to to something similar I get:
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit).build();
BehaviorDelegate<AuthService> delegate = mockRetrofit.create(MyService.class);
如何将呼叫转发到 MyServiceMock
?
推荐答案
假设接口定义如下:
public interface MyService {
@GET("/name")
rx.Observable<String> name();
}
将使用 BehaviorDelegate
将呼叫转发到 MyServiceMock
. BehaviorDelegate
将 NetworkBehavior
应用于模拟创建的响应.请注意,下面的模拟实现将 BehaviorDelegate
作为构造函数参数,并使用它来返回响应.
Calls will be forwarded to MyServiceMock
using BehaviorDelegate
. The BehaviorDelegate
applies NetworkBehavior
to the responses created by the mock. Note that the mock implementation below takes the BehaviorDelegate
as a constructor argument and uses it to return the response.
public class MyServiceMock implements MyService {
private final BehaviorDelegate<MyService> delegate;
public MyServiceMock(BehaviorDelegate<MyService> delegate) {
this.delegate = delegate;
}
public Observable<String> name() {
return delegate.returningResponse("test").name();
}
}
使用模拟服务的测试示例如下所示:
A sample of a test using the mock service is as shown below:
public class MyServiceTest {
private final NetworkBehavior behavior = NetworkBehavior.create();
private final rx.observers.TestSubscriber<String> testSubscriber = TestSubscriber.create();
private MyService mockService;
@Before
public void setUp() throws Exception {
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl("http://example.com").build();
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
.networkBehavior(behavior).build();
final BehaviorDelegate<MyService> delegate = mockRetrofit.create(MyService.class);
mockService = new MyServiceMock(delegate);
}
@Test
public void testSuccessResponse() throws Exception {
givenNetworkFailurePercentIs(0);
mockService.name().subscribe(testSubscriber);
testSubscriber.assertValue("test");
testSubscriber.assertCompleted();
}
@Test
public void testFailureResponse() throws Exception {
givenNetworkFailurePercentIs(100);
mockService.name().subscribe(testSubscriber);
testSubscriber.assertNoValues();
testSubscriber.assertError(IOException.class);
}
private void givenNetworkFailurePercentIs(int failurePercent) {
behavior.setDelay(0, MILLISECONDS);
behavior.setVariancePercent(0);
behavior.setFailurePercent(failurePercent);
}
}
这篇关于如何使用Retrofit 2.0和新的MockRetrofit类模拟服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!