gpt4 book ai didi

java - 如何使用 Mockito 测试 POST 方法

转载 作者:行者123 更新时间:2023-11-30 07:17:57 25 4
gpt4 key购买 nike

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

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) 帖子,以便稍后测试它。

非常感谢最好的问候。

最佳答案

就是这样!!!

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

关于java - 如何使用 Mockito 测试 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15708708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com