【问题标题】:Standalone wiremock server as spring boot application独立的wiremock服务器作为spring boot应用程序
【发布时间】:2021-04-18 14:53:18
【问题描述】:
我正在尝试将模拟服务作为 Spring Boot 应用程序。
我可以在 Spring Boot 应用程序中使用独立的模拟服务器吗?
当我尝试在 Spring Boot 应用程序内的任何端口上运行模拟服务器时,它会抛出“地址已绑定异常”
有没有办法解决这个问题,这样我就可以让一个 mockservice 作为 spring boot docker 容器运行,并且只需配置我想要模拟的 url。
【问题讨论】:
标签:
spring-boot
docker
mocking
mockserver
wiremock-standalone
【解决方案1】:
基本上,您会希望避免使用任何启动容器/服务器的启动器。最初,我只有这两个依赖项:com.github.tomakehurst:wiremock-jre8:2.31.0 和 org.springframework.boot:spring-boot-starter-json。或者,确认您不想运行任何服务器:spring.main.web-application-type: none。
最后,声明一个配置文件来设置 WireMock:
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(WireMockConfig.ApplicationProps.class)
public class WireMockConfig {
private final ApplicationProps props;
@Bean
public WireMockServer mockServer() {
return new WireMockServer(WireMockConfiguration.options().port(8081));
}
}
...和一个启动服务器的监听器:
@Component
@AllArgsConstructor
public class ApplicationListener {
private final WireMockServer server;
@EventListener
public void onApplicationEvent(final ApplicationReadyEvent event) {
server.start();
}
}