Getting 500 error while testing webflux code with wiremock

Solution for Getting 500 error while testing webflux code with wiremock
is Given Below:

I have spring webflux app with user controller class with end point “/user and user service class.The user service class making call to external api. I am trying to test the service class using wiremock and junit 5 to mock out external api.. However I am getting below error ->

021-07-30 18:22:52.511 ERROR 16974 — [o-auto-1-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] threw exception

java.net.ConnectException: Connection refused

Code is uploaded at path : https://github.com/neeleshsethi/wiremockdemp/tree/master

It seems it cannot find controller as adding a print statement in controller is not printing anything. Below is the code ->

@Service
public class UserService {

    @Autowired
    WebClient webClient;


    public Mono<User> createuser(User user) {

       return  webClient.post()
                .uri("/usercreate")
               .contentType(MediaType.APPLICATION_JSON)
               .accept(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(user))
                .retrieve()
                .bodyToMono(User.class);

    }



}


WireMock Inti class:
package com.example.wiremockdemo;


import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;

import java.util.Map;

public class WireMockInit implements ApplicationContextInitializer<ConfigurableApplicationContext> {


    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().dynamicPort());
        wireMockServer.start();
        applicationContext.addApplicationListener( applicationEvent ->
                {
                        if(applicationEvent instanceof ContextClosedEvent)
                        {
                            wireMockServer.stop();

                        }

                }

        );
        applicationContext.getBeanFactory().registerSingleton("wireMockServer", wireMockServer);
       TestPropertyValues.of("externalBaseUrl",wireMockServer.baseUrl())
              .applyTo(applicationContext);






    }


}


test class:
package com.example.wiremockdemo;

import com.example.wiremockdemo.model.User;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Mono;

import java.awt.*;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = {WireMockInit.class})
class WiremockdemoApplicationTests {

    @Autowired
    WebTestClient webTestClient;

    @Autowired
    private WireMockServer wireMockServer;

    @LocalServerPort
    private Integer port;


    @Test
    void createUsertest() {

        System.out.println("Creating stub");
        wireMockServer.stubFor(


                WireMock.post("/usercreate")
                .willReturn(

                        aResponse()
                            .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                            .withBodyFile("response.json"))


        );


    byte[] temp =   webTestClient.post()
                   .uri("http://localhost:" + port + "/user")
                 //    .uri("/user")
            .contentType(MediaType.APPLICATION_JSON)
            //.accept(MediaType.APPLICATION_JSON)
                     .body(BodyInserters.fromValue(createUser()))
                      .exchange()
                      .expectBody()
                     .returnResult()
                .getResponseBody();

    String s = new String(temp);

    System.out.println("Response :" +s);





    }



    public User createUser()
    {
        return  User.builder()
                .firstName("neel")
                .age(32)
                .id(1234)
                .build();
    }


}

Change this line to:

TestPropertyValues.of(Map.of("externalBaseUrl", wireMockServer.baseUrl()));

This is the correct way to set test properties.