欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WireMock快速伪造restful服务

程序员文章站 2024-01-19 11:06:34
...

WireMock:

     快速伪造restful服务,相当于单独的服务器。

主要特点

  • HTTP响应存根,URL,标题和正文内容模式的匹配
  • 请求验证
  • 在单元测试中运行,作为独立进程或作为WAR应用程序运行
  • 可通过流畅的Java API,JSON文件和HTTP上的JSON进行配置
  • 记录/回放存根
  • 故障注入
  • 按请求条件代理
  • 浏览器代理请求检查和替换
  • 有状态行为模拟
  • 可配置的响应延迟

步骤:

项目引用:

  1.先去官网下载 WireMock服务器。

   WireMock快速伪造restful服务

   WireMock快速伪造restful服务

  启动命令:

java -jar wiremock-standalone-2.21.0.jar

WireMock快速伪造restful服务

pom:

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
</dependency>

 测试:

package com.zhw.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.removeAllMappings;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

public class MockServer {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		configureFor(8062);
		removeAllMappings();

		mock("/order/1", "01");
		mock("/order/2", "02");
	}

	private static void mock(String url, String file) throws IOException {
		ClassPathResource resource = new ClassPathResource("mock/response/" + file + ".txt");
		String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "UTF-8").toArray(), "\n");
		stubFor(get(urlPathEqualTo(url)).willReturn(aResponse().withBody(content).withStatus(200)));
	}

}

WireMock快速伪造restful服务WireMock快速伪造restful服务

WireMock快速伪造restful服务

其他用法请看官网。 

 官网:http://wiremock.org/

上一篇: CDH5.0.2升级至CDH5.2.0

下一篇: