1

Since I've upgraded my spring-boot-dependencies version from 2.2.5.RELEASE to 2.4.0, all tests where MockWebServer is used are failing with the following error:

org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/0:0:0:0:0:0:0:1:56338; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56338

Any clue? Wrong depedency used / deprecated stuff?


Maven depedency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.4.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <scope>test</scope>
    <version>5.7.0</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>mockwebserver</artifactId>
    <scope>test</scope>
    <version>3.14.9</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <scope>test</scope>
    <version>3.14.9</version>
</dependency>

I have writen a simple test case inside of my current , but i got always the same issue... (thanks to https://stackoverflow.com/a/62528728/1638483)

import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.web.reactive.function.client.WebClient;

public class QuoteClient {

    private final WebClient webClient;

    public QuoteClient(WebClient.Builder builder, String baseUrl) {
        this.webClient = builder.baseUrl(baseUrl).build();
    }

    public JsonNode getData() {
        return this.webClient
                .get()
                .retrieve()
                .bodyToMono(JsonNode.class)
                .block();
    }
}



import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.reactive.function.client.WebClient;

import static org.junit.Assert.assertNotNull;

public class QuotesClientTest {
    private QuoteClient quotesClient;
    private MockWebServer server;

    @BeforeEach
    public void setup() {
        this.server = new MockWebServer();
        this.quotesClient = new QuoteClient(WebClient.builder(), server.url("/").toString());
    }

    @Test
    public void test() {
        server.enqueue(new MockResponse()
                .setStatus("HTTP/1.1 200")
                .setBody("{\"bar\":\"barbar\",\"foo\":\"foofoo\"}")
                .addHeader("Content-Type", "application/json"));

        JsonNode data = quotesClient.getData();
        assertNotNull(data);

        System.out.println(data);

    }
}

StackTrace

org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382

       at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:137)
       Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
   |_ checkpoint ⇢ Request to GET null [DefaultWebClient]
Stack trace:
       at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:137)
       at reactor.core.publisher.MonoErrorSupplied.subscribe(MonoErrorSupplied.java:70)
       at reactor.core.publisher.Mono.subscribe(Mono.java:3987)
       at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
//...
       at reactor.netty.transport.TransportConnector$MonoChannelPromise.tryFailure(TransportConnector.java:464)
       at reactor.netty.transport.TransportConnector$MonoChannelPromise$1.tryFailure(TransportConnector.java:515)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
       at java.lang.Thread.run(Thread.java:748)
   Suppressed: java.lang.Exception: #block terminated with an error
       at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
       at reactor.core.publisher.Mono.block(Mono.java:1679)
       at QuoteClient.getData(QuoteClient.java:23)
       at QuotesClientTest.test(QuotesClientTest.java:30)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
       at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
       at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
//...
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
       at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
       at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
   Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382
   Caused by: java.net.ConnectException: Connection refused
       at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
       at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
       at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
       at java.lang.Thread.run(Thread.java:748)
gudepier
  • 3,362
  • 6
  • 22
  • 26
  • FYI when I downgrade the spring-boot-dependencies to **2.3.11.RELEASE**, it's working like a charm -_- – gudepier Jun 10 '21 at 15:03

2 Answers2

0

It seems that the issue has been resolved by using the spring-boot-dependencies since the 2.4.5 version (i didn't tested the intermediates versions).


UPDATE:

Pay attention to your local hosts file...

I've updated mine by adding .local as suffix of localhost & <myHostname> because the issue reappeared this morning (without any changes -_-)

127.0.0.1   localhost.local,<myHostname>.local
255.255.255.255 broadcasthost
::1             localhost.local,<myHostname>.local
gudepier
  • 3,362
  • 6
  • 22
  • 26
0

in my case, I had commented this line in my hosts file.

127.0.0.1 localhost

Uncommenting this fixed my problem.