java 如何使用 Mockito 测试 POST 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15708708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test POST method using Mockito
提问by Ignacio Garat
HI have the following test and I cannot make it work:
嗨有以下测试,我不能让它工作:
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));
}
How do I prepare my (Markup.class) Post, so I can test it later on.
我如何准备我的 (Markup.class) 帖子,以便我稍后对其进行测试。
Thank you very much Best Regards.
非常感谢。致以最诚挚的问候。
回答by Ignacio Garat
This was it!!!
原来是这样!!!
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);
回答by Piotr Kochański
Testing web service with Mockito is doable, but if you want to test dynamic behaviour it is much better to use RestAssuredtesting framework. It is mostly designed to test REST web services, but can be also used to test "normal" HTTP form posts.
使用 Mockito 测试 Web 服务是可行的,但如果您想测试动态行为,最好使用RestAssured测试框架。它主要用于测试 REST Web 服务,但也可用于测试“正常”的 HTTP 表单帖子。
If you mock everything with Mockito, you would test mostly your mocks, not a real behaviour.
如果你用 Mockito 模拟一切,你将主要测试你的模拟,而不是真实的行为。
Testing with Mockito would mean that you create mock HTTP request and then pass it to some method which consumes that request. The you can create some assertions that would check if method parsed request properly and gave correct outcome.
使用 Mockito 进行测试意味着您创建模拟 HTTP 请求,然后将其传递给使用该请求的某个方法。您可以创建一些断言来检查方法是否正确解析了请求并给出了正确的结果。