第五节 wireMock的java版应用

一、配置jar包

1)pom.XML配置方法

<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-standalone</artifactId>
    <version>2.18.0</version>
</dependency>

2)Gradle 配置方法

testCompile "com.github.tomakehurst:wiremock-standalone:2.18.0"

二、wireMock在java中的启动

1、使用Junit启动:

1)配置pom.XML文件

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

2)@Rule启动:

示例:

  @Rule
   public WireMockRule wm = new WireMockRule(options().port(2345));

demo: 

package com.qiyi.wireMock.Beging.Configuration;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.qiyi.HTTP.GetNeedHeader;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
public class Excemple_0001_startUpBaseRulePortDemo {
    /**
     *基本用法
     */
    @Rule
   public WireMockRule wm = new WireMockRule(options().port(2345));

   public  Map<String,String> Header = new HashMap<String,String>();
    @Test
    public void exactUrlOnly() throws InterruptedException {
        String host="localhost";
        Integer Port=2345;
       // configureFor(host, Port);
        // mock
        stubFor(get(urlEqualTo("/some/thing"))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "text/plain")
                        .withHeader("Content-Type1", "text/plain1")
                        .withBody("Hello world!")));

        //调用
        Header.put("Content-Type","text/plain");
        Header.put("Content-Type1","text/plain1");

        String url="http://"+host+":"+Port.toString()+"/some/thing";
        System.out.println("url:"+url);
        String StringToJson = GetNeedHeader.sendGet(url,Header);

        System.out.println("基于@Rule的启动:---->"+StringToJson);

        //测试
        Assert.assertEquals("Hello world!",StringToJson);

    }
}

测试通过:

2)非Junit方式启动

Demo:

package com.qiyi.wireMock.Beging.StartUp;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.qiyi.HTTP.GetNeedHeader;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

public class Excemple_0004_startUpNotUseJunit_Client {


    public  Map<String,String> Header = new HashMap<String,String>();
    @Test
    public void exactUrlOnly() throws InterruptedException {
        String host="localhost";
        Integer Port=8089;

        //服务端启动
        WireMockServer wireMockServer = new WireMockServer(8089); //No-args constructor will start on port 8080, no HTTPS
        wireMockServer.start();

        //客户端调用
        configureFor(host, Port);

        //mock
        stubFor(get(urlEqualTo("/some/thing"))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "text/plain")
                        .withHeader("Content-Type1", "text/plain1")
                        .withBody("Hello world!")));

        //配置header
        Header.put("Content-Type","text/plain");
        Header.put("Content-Type1","text/plain1");

        String url="http://"+host+":"+Port.toString()+"/some/thing";
        System.out.println("url:"+url);


        //发送http请求
        String StringToJson = GetNeedHeader.sendGet(url,Header);
        System.out.println("基于@Rule的启动:---->"+StringToJson);


        //测试http
        Assert.assertEquals("Hello world!",StringToJson);



    }
}

测试通过:

3)模拟接口:

使用stubFor方法模拟接口:不同的接口不同的配置方法,详情参见官网:http://wiremock.org/docs/stubbing/

   stubFor(get(urlEqualTo("/some/thing"))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "text/plain")
                        .withHeader("Content-Type1", "text/plain1")
                        .withBody("Hello world!")));

4)github:

猜你喜欢

转载自blog.csdn.net/weixin_39527812/article/details/81563066