1

I am trying to make a mock service as a spring boot application. Can I use standalone mock server inside a spring boot application? When I tried to run a mock server on any port inside the spring boot application it throws the "Address already bound exception" Is there a way to over come that so that I can have a mockservice running as a spring boot docker container and just configure the urls I want to mock.

1 Answer 1

2

Basically, you would want to avoid any starter that brings up a container/server. Initially, I have only these two dependencies: com.github.tomakehurst:wiremock-jre8:2.31.0 and org.springframework.boot:spring-boot-starter-json. Optionally, confirm you don't want to run any server(s): spring.main.web-application-type: none.

Finally, declare a config file to setup 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));
  }
}

...and a listener to start the server:

@Component
@AllArgsConstructor
public class ApplicationListener {
  private final WireMockServer server;

  @EventListener
  public void onApplicationEvent(final ApplicationReadyEvent event) {
    server.start();
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.