WireMock使用

1.进入WireMock 网站 http://wiremock.org/docs/running-standalone/

2. 下载

3.启动服务

 

注意:--port 是指定服务的端口 你可以指定成别的端口。

         其他相关参数设置请阅读官方文档 

4. 引入jar包,我这里引的是SpringCloud 的

  <Springcloud 的版本控制-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.SR2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

    <!-- jar -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-contract-wiremock</artifactId>
    </dependency>

 

package wang.chunsen.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.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

/**
 * @author wangchunsen
 */
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)));
  }

}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值