WireMock提供Restful接口数据

Posted kibana

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WireMock提供Restful接口数据相关的知识,希望对你有一定的参考价值。

1、去官网下载并启动:
技术分享图片

技术分享图片

2、引入Pom依赖:

    <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>25.1-jre</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.6</version>
    </dependency>   
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.6</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.5</version>
    </dependency>

3、编写客户端设置请求与响应:

package wiremock;

import com.github.tomakehurst.wiremock.client.WireMock;

public class WireMockClient {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WireMock.configureFor(8090);
        WireMock.removeAllMappings();
        WireMock.stubFor(
                WireMock.get(WireMock.urlPathEqualTo("/order/1")).
                willReturn(WireMock.aResponse().
                withBody("{"id":1,"name":"cqu2010"}").withStatus(200))); //伪造测试桩
    }

}

4、测试:
技术分享图片



以上是关于WireMock提供Restful接口数据的主要内容,如果未能解决你的问题,请参考以下文章

java后台接口怎么方便返回restful数据

后端提供给前端restful的接口,前端怎么操作

测试工具Wiremock介绍

自动化测试:wiremock api全解析

自动化测试:wiremock api全解析

restful 接口 复杂对象入参