我尝试将wiremock设置为在随机端口上运行https:
@Rule
public WireMockRule wireMockServer = new WireMockRule(
WireMockConfiguration.wireMockConfig().dynamicPort().dynamicHttpsPort()
);
但是当我使用它并且我调用wireMockServer.httpsPort()
时,我得到了例外:
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?
注意:我使用的是版本2.14.0
答案 0 :(得分:0)
我使用了WireMockRules
类,让我的测试类继承它
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())
)
);
}
}