I have java 8 application. I use this maven impor...
# help
r
I have java 8 application. I use this maven import:
Copy code
<dependency>
      <groupId>com.github.tomakehurst</groupId>
      <artifactId>wiremock-standalone</artifactId>
      <version>2.27.2</version>
      <scope>test</scope>
    </dependency>
I want to create a mock server and test if expected header was actually posted. To do this I have invented the following code:
Copy code
@Test
  void testHeaders() throws InterruptedException {

        String expectedBody = "Expected body";
        String expectedPath = "/expectedPath";
        String expectedHeaderName = "X-Custom-Header";
        String expectedHeaderValue = "Custom Value";

        wireMockServer = new WireMockServer(WireMockConfiguration.options().port(8080));
        httpClient = HttpClients.createDefault();
        wireMockServer.start();

        StubMapping stubMapping = wireMockServer.stubFor(WireMock.get(WireMock.urlEqualTo(expectedPath))
            .withHeader(expectedHeaderName, WireMock.equalTo(expectedHeaderValue))
            .willReturn(WireMock.aResponse()));

        int i = 10_000;
        //Sleep in loop to get  some time to make request from curl
        while (i > 0){Thread.sleep(10);i -=100;}

        WireMock.verify(WireMock.getRequestedFor(WireMock.urlEqualTo(expectedPath)).withHeader(expectedHeaderName, WireMock.equalTo(expectedHeaderValue)));

        try {
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        wireMockServer.stop();
  }
What's the good way to run it in background? For example if I just put breakpoint directly WireMock.verify without having while (i > 0){Thread.sleep(10);i -=100;} in my code then I cannot make requests to server from curl despite the fact that. wireMockServer.stop() has not yet been called. Is there any option to start it in new thread or are developers supposed to manually deal with creating threads?
l
Hi, I am a little confused as to why you are trying to send
curl
requests to the server? The normal pattern with these types of tests is to : 1. Setup the WireMock Server 2. Create the stub for the request you are testing 3. Execute the code that makes the request. This is normally via executing some application code (in your case the test doesn't do this. You create a
httpClient
but don't make any requests to the server or execute any application code) 4. Verify the request to Wiremock If you are wanting to send requests to a WireMock server via
curl
or postman then you might want to take a look at WireMock standalone. This will allow you to spin up a WireMock server and then send requests to it using any client you choose. https://wiremock.org/docs/standalone/java-jar/
1