如何使WireMock端口更具动态性以将其用于测试服务

nie*_*mar 5 spring github-api spring-boot wiremock

我正在使用Wiremock模拟github api对我的服务进行一些测试。该服务调用API GitHub的。为了测试我的端点属性设置为

github.api.endpoint=http://localhost:8087
Run Code Online (Sandbox Code Playgroud)

该主机和端口与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);
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ner 15

您必须将 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 {
    ....
}
Run Code Online (Sandbox Code Playgroud)

另请参阅Spring Cloud Contract WireMock


dha*_*310 9

另一种方法,您可以使用动态端口而不会发生冲突

import org.springframework.util.SocketUtils;

int WIREMOCK_PORT = SocketUtils.findAvailableTcpPort();

public WireMockRule wireMockServer = new WireMockRule(WIREMOCK_PORT);
Run Code Online (Sandbox Code Playgroud)

如果你想从属性文件中访问它,那么我们wiremock.server.port提供了 Wiremock

"github.api.endpoint=http://localhost:${wiremock.server.port}"
Run Code Online (Sandbox Code Playgroud)


Los*_*ana 8

我知道这是一个有点旧的帖子,但仍然有一种动态拥有这些端口的记录方法。在此处阅读更多信息:入门。只需向下滚动到“随机端口号”即可。从那里的文档:

你需要做的是像这样定义一个规则

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());  
Run Code Online (Sandbox Code Playgroud)

然后通过访问它们

int port = wireMockRule.port();
int httpsPort = wireMockRule.httpsPort();
Run Code Online (Sandbox Code Playgroud)


bes*_*hes 3

我不知道,@AutoConfigureWireMock但如果你手动启动wiremock并设置模拟,在启动spring时你可以使用spring random设置一个随机端口号。示例如下所示

在你的wiremock类中

@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();
   }

}
Run Code Online (Sandbox Code Playgroud)

在你的测试课中

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

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

   //use the above url to get stubbed responses.
}
Run Code Online (Sandbox Code Playgroud)

在你的 application.properties 文件中

randomportnumber=${random.int[1,9999]}
github.api.endpoint=http://localhost:${randomportnumber}
Run Code Online (Sandbox Code Playgroud)