2016-06-20 2 views
4

Hat jemand eine vollständige und aktuelle Tutorial oder Projekt auf Aufnahme-Server-Antworten-Get und Post und Header nach Bedarf und Wiedergabe sie mit Wiremock und/oder Mockwebserver?Mocking Server Ergebnisse auf Android (Wiremock, MockWebServer etc)

Ich habe an vielen sah da draußen schon

+0

wie hast du schon in viele geschaut, was fehlt dir noch? – djodjo

+0

Unter http://wiremock.org/docs/record-playback/ finden Sie eine grundlegende Anleitung zum Aufzeichnen und Wiedergeben von http-Kommunikationen. Bitte fragen Sie genauer, wenn Sie Probleme mit einem Schritt haben. – nenick

Antwort

2

ich einen in meinem derzeitigen Projekt umgesetzt hatte. Zunächst einmal habe ich eine MockHTTPDispatcher Klasse erstellt, die Klasse von OkHttp3 erweitert. Der Matcher wird hier aus Hamcrest importiert.

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 
} 

Dann habe ich eine Klasse, die MockWebServerRuleTestRule implementiert. Dieser Codeabschnitt deckt einen Fall ab, wenn Sie Header festlegen möchten und wenn nicht.

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(); 
      } 
     } 
    } 
} 

Für die httpMethodIs und pathStartsWith, benötigen Sie eine Methode wie diese irgendwo erstellen (i erstellt eine Klasse RequestMatchers für diese genannt).

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"); 
     } 
    }; 
} 

Und

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"); 
     } 
    }; 
} 

In Ihrer Instrumentierung Test können Sie die Mock Webserver Regel @Rule Annotation verwenden rufen Sie einfach wie folgt aus:

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 
    } 
} 

Sie können nur die "GET" mit "POST" ändern oder alles andere mit Ihrer erwarteten Statuscodeantwort. Vergessen Sie nicht, die Implementierung von readAsset(String fileName) irgendwo in Ihren Code einzufügen, damit sie die jSON-Assets, die Sie gespeichert haben, lesen kann.