Skip to main content
improve method to start wiremock server
Source Link
user4695271
user4695271

Basically, you would't need almostwould want to avoid any Spring Boot dependencies (initially), except contextstarter that brings up a container/server.

  Initially, I do use this starterhave only these two dependencies: com.github.tomakehurst:wiremock-jre8:2.31.0 and org.springframework.boot:spring-boot-starter-json.

Then state Optionally, confirm you don't want to run any Web applicationserver(s): spring.main.web-application-type: none. Declare

Finally, declare a WireMockServer beanconfig file to setup WireMock:

@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(WireMockConfig.ApplicationProps.class)
public class WireMockConfig {
  private final ApplicationProps props;

  @Bean
  public WireMockServer mockServer() {
  final var configurationreturn =new WireMockServer(WireMockConfiguration.options().port(props.wiremock.port8081));
  return new WireMockServer(configuration);}
}

...and a listener to start thatthe server, either on mockServer() or using some other way.:

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

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

Basically, you would't need almost any Spring Boot dependencies (initially), except context.

  I do use this starter: org.springframework.boot:spring-boot-starter-json.

Then state you don't want any Web application: web-application-type: none. Declare a WireMockServer bean:

@Bean
public WireMockServer mockServer() {
  final var configuration = WireMockConfiguration.options().port(props.wiremock.port);
  return new WireMockServer(configuration);
}

...and start that server, either on mockServer() or using some other way.

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();
  }
}
Source Link
user4695271
user4695271

Basically, you would't need almost any Spring Boot dependencies (initially), except context.

I do use this starter: org.springframework.boot:spring-boot-starter-json.

Then state you don't want any Web application: web-application-type: none. Declare a WireMockServer bean:

@Bean
public WireMockServer mockServer() {
  final var configuration = WireMockConfiguration.options().port(props.wiremock.port);
  return new WireMockServer(configuration);
}

...and start that server, either on mockServer() or using some other way.