寻找在android测试中模拟api响应的方法。
我已经阅读了robolectric可以用于此,但我真的很感激任何建议。
答案 0 :(得分:2)
在网上浏览一下后,我发现MockWebServer就是我想要的。
用于测试HTTP客户端的可编写脚本的Web服务器。使用此库可以轻松测试应用程序在进行HTTP和HTTPS调用时是否正确。它允许您指定要返回的响应,然后验证请求是否按预期进行。
要进行设置,只需将以下内容添加到 build.gradle 文件中。
androidTestCompile 'com.google.mockwebserver:mockwebserver:20130706'
以下是一个从GitHub页面获取的简单示例。
public void test() throws Exception {
// Create a MockWebServer. These are lean enough that you can create a new
// instance for every unit test.
MockWebServer server = new MockWebServer();
// Schedule some responses.
server.enqueue(new MockResponse().setBody("hello, world!"));
// Start the server.
server.play();
// Ask the server for its URL. You'll need this to make HTTP requests.
URL baseUrl = server.getUrl("/v1/chat/");
// Exercise your application code, which should make those HTTP requests.
// Responses are returned in the same order that they are enqueued.
Chat chat = new Chat(baseUrl);
chat.loadMore();
assertEquals("hello, world!", chat.messages());
// Shut down the server. Instances cannot be reused.
server.shutdown();
}
希望这有帮助。
答案 1 :(得分:2)
使用AndroidTestCase,MockWebServer不适用于我。例如,ECONNREFUSED错误非常随机地发生(在https://github.com/square/okhttp/issues/1069中描述)。我没有尝试过Robolectric。
从OkHttp 2.2.0开始,我找到了另一种适用于我的方法:拦截器。我将整个模拟响应放在androidTest/assets/
存储的json文件中,比如'mock_response.json'。当我实例化一个OkHttp进行测试时,我暴露了一个Interceptor,我会重写传入的响应。基本上,body()会在'mock_response.json'中传输数据。
public class FooApiTest extends AndroidTestCase {
public void testFetchData() throws InterruptedException, IOException {
// mock_response.json is placed on 'androidTest/assets/'
final InputStream stream = getContext().getAssets().open("mock_response.json");
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
return new Response.Builder()
.protocol(Protocol.HTTP_2)
// This is essential as it makes response.isSuccessful() returning true.
.code(200)
.request(chain.request())
.body(new ResponseBody() {
@Override
public MediaType contentType() {
return null;
}
@Override
public long contentLength() {
// Means we don't know the length beforehand.
return -1;
}
@Override
public BufferedSource source() {
try {
return new Buffer().readFrom(stream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
})
.build();
}
});
FooApi api = new FooApi(httpClient);
api.fetchData();
// TODO: Let's assert the data here.
}
}
答案 2 :(得分:1)
现在Mockinizer变得更容易,这使得使用MockWebServer更加容易:
val mocks: Map<RequestFilter, MockResponse> = mapOf(
RequestFilter("/mocked") to MockResponse().apply {
setResponseCode(200)
setBody("""{"title": "Banana Mock"}""")
},
RequestFilter("/mockedError") to MockResponse().apply {
setResponseCode(400)
}
)
只需创建一个RequestFilter和MockResponses的映射,然后将其插入到OkHttpClient构建器链中即可:
OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.mockinize(mocks) // <-- just plug in your custom mocks here
.build()