java 随机端口,如何配置WireMock使用https(和随机端口)?

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

@Rule

public WireMockRule wireMockServer = new WireMockRule(

WireMockConfiguration.wireMockConfig().dynamicPort().dynamicHttpsPort()

);

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)

How do i set WireMock to use https?

NOTE: I'm using version 2.14.0

解决方案

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()

);

}

and my 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())

)

);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值