2

嗨有以下测试,我不能让它工作:

protected HttpClient mHttpClientMock;
protected HttpPost mHttpPostMock;
protected HttpResponse mHttpResponseMock;
protected StatusLine mStatusLineMock;
protected HttpEntity mHttpEntityMock;
protected ContentResolver mResolver;

    protected void setUp() throws Exception {
    super.setUp();
    // Create mocks.
    mHttpPostMock = Mockito.mock(HttpPost.class);
    mHttpClientMock = Mockito.mock(HttpClient.class);
    mHttpResponseMock = Mockito.mock(HttpResponse.class);
    mStatusLineMock = Mockito.mock(StatusLine.class);
    mHttpEntityMock = Mockito.mock(HttpEntity.class);

    prepareMocks();

    // Obtain Content Resolver.
    mResolver = getContext().getContentResolver();
}


protected void prepareMocks() throws IOException {
    // Create mocked response.
    // Define expected calls.
    Mockito.when(mHttpClientMock.execute(Mockito.isA(HttpPost.class)));
//Mockito.when(mHttpResponseMock.getStatusLine()).thenReturn(mStatusLineMock);
//  Mockito.when(mStatusLineMock.getStatusCode()).thenReturn(HttpStatus.SC_OK);
//  Mockito.when(mHttpResponseMock.getEntity()).thenReturn(mHttpEntityMock);
    Mockito.when(mHttpClientMock.execute(Mockito.mock(Markup.class)));
 //  Mockito.when(mHttpEntityMock.writeTo(Mockito.mock(Markup.class));
}

我如何准备我的(Markup.class)帖子,以便稍后进行测试。

非常感谢。致以最诚挚的问候。

4

2 回答 2

4

就是这个!!!

        InputStream jsonResponse = createJsonResponse();
    // Define expected calls.
    Mockito.when(mHttpClientMock.execute(Mockito.isA(HttpPost.class))).thenReturn(mHttpResponseMock);
    Mockito.when(mHttpResponseMock.getStatusLine()).thenReturn(mStatusLineMock);
    Mockito.when(mStatusLineMock.getStatusCode()).thenReturn(HttpStatus.SC_OK);
    Mockito.when(mHttpResponseMock.getEntity()).thenReturn(mHttpEntityMock);
    Mockito.when(mHttpEntityMock.getContent()).thenReturn(jsonResponse);
于 2013-03-30T02:52:25.673 回答
0

使用 Mockito 测试 Web 服务是可行的,但如果你想测试动态行为,最好使用RestAssured测试框架。它主要用于测试 REST Web 服务,但也可用于测试“正常”HTTP 表单帖子。

如果你用 Mockito 模拟一切,你将主要测试你的模拟,而不是真正的行为。

使用 Mockito 进行测试意味着您创建模拟 HTTP 请求,然后将其传递给使用该请求的某个方法。您可以创建一些断言来检查方法是否正确解析请求并给出正确的结果。

于 2013-03-29T18:39:20.560 回答