使用mockserver来进行http接口mock
Posted Silentdoer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用mockserver来进行http接口mock相关的知识,希望对你有一定的参考价值。
转载自:https://blog.csdn.net/heymysweetheart/article/details/52227379;(注,这个不是很符合我的要求,它主要的作用是可以通过简单的代码就能filter对方的请求options,然后做出对应的响应;而我需要的是一个能够开启为http服务端来测试客户端发来的http请求的工具,然后顺便看到了这篇感觉以后可能也会用到)
前言
进行单元测试时,必须要mock掉第三方的依赖调用,而mockserver提供了足够的api来支持这种http的mock,现在简单介绍如何使用mockserver进行http接口mock。
依赖
- mockserver依赖
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>3.10.4</version>
</dependency>
- httpclient依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
httpclient的版本可能会出现不一致的情况,抛出异常,以上的这个版本匹配是没有异常的组合。
举例
package com.yuliang.dubbo.service;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Rule;
import org.junit.Test;
import org.mockserver.client.server.MockServerClient;
import org.mockserver.junit.MockServerRule;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
/**
* Created by leo on 16/8/16.
*/
public class MockServerTest {
@Rule
public MockServerRule server = new MockServerRule(this, 5000);
@Test
public void testMockServer() throws IOException {
MockServerClient mockClient = new MockServerClient("localhost", 5000);
String expected = "{ message: ‘incorrect username and password combination‘ }";
mockClient.when(
request()
.withPath("/hello/John")
.withMethod("GET")
// .withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))
// .withQueryStringParameter(new Parameter("my-token", "12345"))
).respond(
response()
.withStatusCode(200)
.withBody(expected)
);
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:5000/hello/John");
CloseableHttpResponse response = client.execute(httpGet);
//验证输出是否是正确
InputStream content = response.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(content);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String responseText = bufferedReader.readLine();
assertThat(responseText, equalTo(expected));
}
}
-
mock http post请求
@Test
public void testPostMockServer() throws IOException {
MockServerClient mockClient = new MockServerClient("localhost", 5000);
String expected = "You have logged in successfully.";
mockClient.when(
request()
.withPath("/hello/John")
.withMethod("POST")
.withHeader(new Header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN))
// .withQueryStringParameter(new Parameter("my-token", "12345"))
.withBody("username=foo&password=123456")
).respond(
response()
.withStatusCode(200)
.withBody(expected)
);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:5000/hello/John");
httpPost.addHeader("Accept", "text/plain");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "foo"));
nameValuePairs.add(new BasicNameValuePair("password", "123456"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
CloseableHttpResponse response = client.execute(httpPost);
//验证输出是否是正确
InputStream content = response.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(content);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String responseText = bufferedReader.readLine();
assertThat(responseText, equalTo(expected));
}
以上是关于使用mockserver来进行http接口mock的主要内容,如果未能解决你的问题,请参考以下文章