如何配置WireMock使用https(和随机端口)? [英] How configure WireMock to use https (and a random port)?

查看:832
本文介绍了如何配置WireMock使用https(和随机端口)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置wiremock在随机端口上运行https:

I tried to set wiremock to run https on a random port:

@Rule
public WireMockRule wireMockServer = new WireMockRule(
    WireMockConfiguration.wireMockConfig().dynamicPort().dynamicHttpsPort()
);

但是当我使用它并且我调用 wireMockServer.httpsPort()我得到异常:

but when I use this and I call wireMockServer.httpsPort() I get the exception:

java.lang.IllegalStateException: Not listening on HTTPS port. Either HTTPS is not enabled or the WireMock server is stopped.
    at com.google.common.base.Preconditions.checkState(Preconditions.java:150)
    at com.github.tomakehurst.wiremock.WireMockServer.httpsPort(WireMockServer.java:184)

如何设置WireMock以使用https?

How do i set WireMock to use https?

注意:我使用的是版本2.14.0

推荐答案

我使用的是 WireMockRules 类并让我的测试类继承它

I used a WireMockRules class and had my test classes inherit from it

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class WiremockRules {
    @Rule
    public WireMockRule wireMockRule = new WireMockRule(
        wireMockConfig().dynamicPort().dynamicHttpsPort()
    );
}

和我的 WiremockTest.java

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest()
public class WiremockTest extends WiremockRules {

    private String url;

    @Before
    public void setup() {
        url = baseUrl + Integer.toString(wireMockRule.port()) + "/v1/test";

        stubFor(
            get(urlEqualTo(url))
                .willReturn(
                    aResponse()
                        .withStatus(HttpStatus.OK.value())
                )
        );
    }
}

这篇关于如何配置WireMock使用https(和随机端口)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆