title: How to test WireMockServer in JUnit5?
body: <p>I am trying to write a mini-library for testing to mock common external services such as E-mail, SFTP, Buckets, HTTP APIs.</p>
  <p>At the moment, I got stuck on <code>WireMockServer</code>. In <a href="http://wiremock.org/docs/java-usage/" rel="nofollow noreferrer">WireMock docs</a> it states that I can create both server and client to verify API calls.</p>
  <p>I wrote the class:</p>
  <pre><code>public class WireMockTestServer {
  
      private final WireMockServer server;
  
      public WireMockTestServer(String address, MappingBuilder mappingBuilder) {
          server = new WireMockServer(wireMockConfig().dynamicPort().dynamicHttpsPort());
      }
  
      public WireMockTestServer(int httpPort, int httpsPort, String address, MappingBuilder mappingBuilder) {
          server = setup(
                  new WireMockServer(wireMockConfig().port(httpPort).httpsPort(httpsPort).bindAddress(address)),
                  mappingBuilder
          );
      }
  
      private WireMockServer setup(WireMockServer server, MappingBuilder mappingBuilder) {
          server.stubFor(mappingBuilder);
          return server;
      }
  
      public void start() {
          server.start();
      }
  
      public void stop() {
          server.stop();
      }
  }
  </code></pre>
  <p>which I can path endpoint declaration and redirect my services toward it.</p>
  <p>When I am trying to test it:</p>
  <pre><code>public class WireMockTestServerTest {
  
      @Test
      public void testSetup() throws Exception {
          MappingBuilder mappingBuilder = get(urlEqualTo(&quot;/health&quot;))
                  .willReturn(aResponse().withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)
                          .withStatus(200));
          WireMockTestServer server = new WireMockTestServer(8888, 9988, &quot;127.0.0.1&quot;, mappingBuilder);
          server.start();
  
  
          // This line should fail
          verify(getRequestedFor(urlEqualTo(&quot;/health&quot;)).withHeader(&quot;Content-Type&quot;, equalTo(&quot;text/xml&quot;)));
          server.stop();
      }
  
  }
  </code></pre>
  <p>The test fails. The issue is, it fails not because of an assertion but because it starts on a wrong port 8080 which is occupied by other processes.</p>
  <p>How can I start <code>WireMockServer</code> on another port and test it with JUnit 5?</p>
  <p>I am using Java 8, Maven, Spring Boot.</p>