如何使用 Retrofit 2.0 和新的 MockRetrofit class 模拟服务?
How to mock services with Retrofit 2.0 and the new MockRetrofit class?
我们使用 Retrofit 1 来模拟 Web 服务并模拟网络延迟,如下所示:
MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
return mockRestAdapter.create(MyService.class, new MyServiceMock());
其中 MyService
是服务接口(返回响应作为 Rx Observables),MyServiceMock
是实现此接口的 class。
在 Retrofit 2.0.0-beta3 中,有一个尚未记录的全新模拟系统(参见:https://github.com/square/retrofit/pull/1343)。
当尝试做类似的事情时,我得到:
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
作为构造函数参数并将其用于 return 响应。
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();
}
}
使用模拟服务的测试示例如下所示:
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 1 来模拟 Web 服务并模拟网络延迟,如下所示:
MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
return mockRestAdapter.create(MyService.class, new MyServiceMock());
其中 MyService
是服务接口(返回响应作为 Rx Observables),MyServiceMock
是实现此接口的 class。
在 Retrofit 2.0.0-beta3 中,有一个尚未记录的全新模拟系统(参见:https://github.com/square/retrofit/pull/1343)。 当尝试做类似的事情时,我得到:
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
作为构造函数参数并将其用于 return 响应。
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();
}
}
使用模拟服务的测试示例如下所示:
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);
}
}