wiremockwiremock-standalone

Why is this wiremock setup based on wireMockConfig not working?


Why is this wiremock setup based on wireMockConfig not working?

package com.example.wiremockjunitstandalone;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.admin.model.ListStubMappingsResult;
import com.github.tomakehurst.wiremock.client.WireMock;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;

@DirtiesContext
@Slf4j
@SpringBootTest(classes = {WiremockJunitStandaloneApplication.class})
public class WireMockStubbing {

  public static WireMockServer wireMockServer;

  @BeforeAll
  public static  void setUp() {
    // Start WireMock server with the desired hostname and port
    wireMockServer = new WireMockServer(wireMockConfig().port(9999).bindAddress("localhost"));
    wireMockServer.start();
  }

  @AfterAll
  public static void tearDown() {
    // Stop WireMock server after all tests are done
    wireMockServer.stop();
  }

  @Test
  @SneakyThrows
  void onlyPath_localhost() {

    //ignore below
    ListStubMappingsResult stubslist = WireMock.listAllStubMappings();
    System.err.println("stubslist.getMappings().size(): " + stubslist.getMappings().size());
    System.err.println("stubslist.getMappings(): " + stubslist.getMappings());

    HttpRequest request;
    HttpResponse<String> response;

    stubFor(get("/get").willReturn(ok("Hello")));

    request = HttpRequest.newBuilder()
        .uri(new URI("http://localhost:9999/get"))
        .version(HttpClient.Version.HTTP_2)
        .GET()
        .build();
    response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

    Assertions.assertEquals("Hello", response.body());

  }

I am facing below error -

Connect to http://localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect wiremock.org.apache.hc.client5.http.HttpHostConnectException: Connect to http://localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

Why is port 8080 being considered, when port 9999 is explicitly configured. I checked by debuging the port of 9999 is what is taken in options.. so still why 8080 is coming in between. enter image description here


Solution

  • If you're using the static DSL and change the port number you have to tell the client part which port you're using by calling WireMock.configureFor(wireMockServer.port()). You need to do this after start() and before any calls to stubFor(...) etc.