上のサーバーの結果をモック誰もが、必要に応じて取得し、ポストとヘッダresponses-サーバーを記録し、wiremockおよび/またはmockwebserverでそれらを再生する上で完全かつ最新のチュートリアルやプロジェクトを持っていますか?は、Android(Wiremock、MockWebServerなど)
私は私が私の現在のプロジェクトに1つを実装していた、すでにそこに多くの
上のサーバーの結果をモック誰もが、必要に応じて取得し、ポストとヘッダresponses-サーバーを記録し、wiremockおよび/またはmockwebserverでそれらを再生する上で完全かつ最新のチュートリアルやプロジェクトを持っていますか?は、Android(Wiremock、MockWebServerなど)
私は私が私の現在のプロジェクトに1つを実装していた、すでにそこに多くの
を見てきました。まず、MockHTTPDispatcher
クラスを作成して、OkHttp3からDispatcher
クラスまで拡張しました。ここのMatcherはHamcrest
からインポートされます。
public class MockHTTPDispatcher extends Dispatcher {
private Map<Matcher<RecordedRequest>, MockResponse> requestResponseMap;
public MockHTTPDispatcher() {
requestResponseMap = new HashMap<>();
}
public MockHTTPDispatcher mock(Matcher<RecordedRequest> matcher, MockResponse mockResponse) {
requestResponseMap.put(matcher, mockResponse);
return this;
}
@Override
public MockResponse dispatch(RecordedRequest recordedRequest) {
String recordedRequestPath = recordedRequest.getPath();
for (Matcher<RecordedRequest> mockRequest : requestResponseMap.keySet()) {
if (mockRequest.matches(recordedRequest)) {
MockResponse response = requestResponseMap.get(mockRequest);
return response;
}
}
//means unable to find path for recordedRequestPath
return new MockResponse().setResponseCode(404);
}
//you can also create a clear() method to clear the requestResponseMap if needed
}
その後、私はTestRule
を実装MockWebServerRule
クラスを作成しました。この一連のコードは、ヘッダーを設定する場合とそうでない場合をカバーします。 httpMethodIs
とpathStartsWith
については
public class MockWebServerRule implements TestRule {
public static final String DOMAIN = "localhost";
public static final int PORT = 4567;
private MockHTTPDispatcher mockHTTPDispatcher;
private MockWebServer mockWebServer;
public MockWebServerRule() {
mockWebServer = new MockWebServer();
mockHTTPDispatcher = new MockHTTPDispatcher();
mockWebServer.setDispatcher(mockHTTPDispatcher);
}
@Override
public Statement apply(Statement statement, Description description) {
return new MockHTTPServerStatement(statement);
}
public void mockResponse(String path, String httpMethod, int httpResponseCode, String response) throws Exception {
mockResponseWithHeaders(path, httpMethod, httpResponseCode, response, null);
}
public void mockResponseWithHeaders(String path, String httpMethod, int httpResponseCode,
String response, Headers headers) throws Exception {
mock(allOf(pathStartsWith(path), httpMethodIs(httpMethod)), httpResponseCode, response, headers);
}
public void mock(Matcher<RecordedRequest> requestMatcher, int httpResponseCode, String response, Headers headers) throws IOException {
MockResponse mockResponse = new MockResponse()
.setResponseCode(httpResponseCode)
.setBody(response);
if (headers != null)
mockResponse.setHeaders(headers);
mockHTTPDispatcher.mock(requestMatcher, mockResponse);
}
public MockHTTPDispatcher getDispatcher() {
return mockHTTPDispatcher;
}
//inner class for starting and shutting down localhost
private class MockHTTPServerStatement extends Statement {
private Statement base;
public MockHTTPServerStatement(Statement base) {
this.base = base;
}
@Override
public void evaluate() throws Throwable {
mockWebServer.start(PORT);
try {
this.base.evaluate();
} finally {
mockWebServer.shutdown();
}
}
}
}
、あなたは(私はこれらのためにRequestMatchers
という名前のクラスを作成した)このどこかのようなメソッドを作成する必要があります。あなたの計装試験で
public static Matcher<RecordedRequest> httpMethodIs(final String httpMethod) {
return new TypeSafeMatcher<RecordedRequest>() {
@Override
protected boolean matchesSafely(RecordedRequest item) {
return httpMethod.equals(item.getMethod());
}
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("getMethod should return");
}
};
}
そして
public static Matcher<RecordedRequest> pathStartsWith(final String path) {
return new TypeSafeMatcher<RecordedRequest>() {
@Override
protected boolean matchesSafely(RecordedRequest item) {
return item.getPath().startsWith(path);
}
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText("getPath should return");
}
};
}
、あなただけのように注釈@Rule
を使用してモックウェブサーバルールを呼び出すことができます。
public class YourActivityTest {
@Rule
public MockWebServerRule mockWebServerRule = new MockWebServerRule();
@Test
public void shouldHandleResponse200() throws Exception {
mockWebServerRule.mockResponse("/your/api/endpoint/", "GET", 200, readAsset("response_success.json"));
//your assertion here
}
}
あなただけの"POST"
で"GET"
を変更することができますかあなたの期待されたステータスコード応答を持つもの。コードのどこかにreadAsset(String fileName)
の実装を追加することを忘れないでください。これにより、保存したjsonアセットを読み取ることができます。すでに多くに見えたとして
、あなたはまだ何が欠けていますか? – djodjo
http://wiremock.org/docs/record-playback/で、http通信の記録と再生の基本的なチュートリアルがあります。ステップに問題がある場合は、具体的に質問してください。 – nenick