Spring Boot 我怎样才能使WireMock端口更动态地用于测试服务

osh3o9ms  于 2023-04-06  发布在  Spring
关注(0)|答案(5)|浏览(164)

我使用wiremock来模拟github API来测试我的服务。服务调用github api。对于测试,我将endpoint属性设置为

github.api.endpoint=http://localhost:8087

此主机和端口与wiremock服务器@AutoConfigureWireMock(port = 8087)相同,因此我可以测试不同的场景,例如:响应格式错误、超时等。
我怎样才能使这个端口动态,以避免当它已经被系统使用时的情况?有没有一种方法可以在测试中获得wiremock端口并重新分配端点属性?

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 8087)
@TestPropertySource(properties ={"github.api.endpoint=http://localhost:8087"}) 
public class GithubRepositoryServiceTestWithWireMockServer {

@Value("${github.api.client.timeout.milis}")
private int githubClientTimeout;

@Autowired
private GithubRepositoryService service;

@Test
public void getRepositoryDetails() {
    GithubRepositoryDetails expected = new GithubRepositoryDetails("niemar/xf-test", null,
            "https://github.com/niemar/xf-test.git", 1, "2016-06-12T18:46:24Z");
    stubFor(get(urlEqualTo("/repos/niemar/xf-test"))
            .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("/okResponse.json")));

    GithubRepositoryDetails repositoryDetails = service.getRepositoryDetails("niemar", "xf-test");

    Assert.assertEquals(expected, repositoryDetails);
}

@Test
public void testTimeout() {
    GithubRepositoryDetails expected = new GithubRepositoryDetails("niemar/xf-test", null,
            "https://github.com/niemar/xf-test.git", 1, "2016-06-12T18:46:24Z");
    stubFor(get(urlEqualTo("/repos/niemar/xf-test"))
            .willReturn(aResponse()
                    .withHeader("Content-Type", "application/json")
                    .withBodyFile("/okResponse.json")
                    .withFixedDelay(githubClientTimeout * 3)));

    boolean wasExceptionThrown = false;
    try {
        GithubRepositoryDetails repositoryDetails = service.getRepositoryDetails("niemar", "xf-test");
    } catch (GithubRepositoryNotFound e) {
        wasExceptionThrown = true;
    }

    Assert.assertTrue(wasExceptionThrown);
}
zzwlnbp8

zzwlnbp81#

您必须将WireMock端口设置为0,以便它选择一个随机端口,然后使用对此端口的引用(wiremock.server.port)作为端点属性的一部分。

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 0)
@TestPropertySource(properties = {
    "github.api.endpoint=http://localhost:${wiremock.server.port}"
}) 
public class GithubRepositoryServiceTestWithWireMockServer {
    ....
}

Spring Cloud Contract WireMock。

9rygscc1

9rygscc12#

我知道这是一个有点旧的职位,但仍然有一个记录的方式,有这些端口动态.阅读更多这里:Getting started。只需向下滚动一点到“随机端口号”。从文档中可以看到:
你需要做的是定义一个规则

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

然后通过

int port = wireMockRule.port();
int httpsPort = wireMockRule.httpsPort();
jslywgbw

jslywgbw3#

还有一种方法,你可以使用动态端口没有冲突是

import org.springframework.util.SocketUtils;

int WIREMOCK_PORT = SocketUtils.findAvailableTcpPort();

public WireMockRule wireMockServer = new WireMockRule(WIREMOCK_PORT);

如果您想从属性文件访问它,那么我们有Wiremock提供的wiremock.server.port

"github.api.endpoint=http://localhost:${wiremock.server.port}"
zf9nrax1

zf9nrax14#

我不知道@AutoConfigureWireMock,但如果您手动启动wiremock并设置模拟,同时启动spring,您可以使用spring random设置随机端口号。
在你的无线电课上

@Component
public class wiremock {
    @Value("${randomportnumber}")
    private int wiremockPort;

   public void startWiremockServer() {
        WireMock.configureFor("localhost", wiremockPort);
        wireMockServer = new com.github.tomakehurst.wiremock.WireMockServer(wireMockConfig().port(wiremockPort).extensions
                (MockedResponseHandler.class));
        wireMockServer.start();
   }

}

在你的测试课上

//however you want to configure spring
public class wiremock {

    @Value("${github.api.endpoint}")
    private String wiremockHostUrl;

   //use the above url to get stubbed responses.
}

在application.properties文件中

randomportnumber=${random.int[1,9999]}
github.api.endpoint=http://localhost:${randomportnumber}
pgky5nke

pgky5nke5#

如果你使用的是.NET/C#,你可以用空参数启动WireMock服务器,如下所示:

var myMockServer = WireMockServer.Start();

然后获取端口号,如果你需要的话,就像这样:

int portNumber = myMockServer.Port();

相关问题