diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java index 7191dcf44a..ac09e2db42 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java @@ -16,11 +16,11 @@ package org.springframework.boot.web.embedded.netty; -import java.net.BindException; import java.time.Duration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import reactor.netty.ChannelBindException; import reactor.netty.DisposableServer; import reactor.netty.http.HttpResources; import reactor.netty.http.server.HttpServer; @@ -69,8 +69,9 @@ public class NettyWebServer implements WebServer { this.disposableServer = startHttpServer(); } catch (Exception ex) { - if (findBindException(ex) != null) { - throw new PortInUseException(getPort()); + ChannelBindException bindException = findBindException(ex); + if (bindException != null) { + throw new PortInUseException(bindException.localPort()); } throw new WebServerException("Unable to start Netty", ex); } @@ -87,11 +88,11 @@ public class NettyWebServer implements WebServer { return this.httpServer.handle(this.handlerAdapter).bindNow(); } - private BindException findBindException(Exception ex) { + private ChannelBindException findBindException(Exception ex) { Throwable candidate = ex; while (candidate != null) { - if (candidate instanceof BindException) { - return (BindException) candidate; + if (candidate instanceof ChannelBindException) { + return (ChannelBindException) candidate; } candidate = candidate.getCause(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java index 55c8a44678..fb642a3d93 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/netty/NettyReactiveWebServerFactoryTests.java @@ -18,14 +18,14 @@ package org.springframework.boot.web.embedded.netty; import java.util.Arrays; -import org.junit.Ignore; +import org.hamcrest.Matchers; import org.junit.Test; import org.mockito.InOrder; import reactor.netty.http.server.HttpServer; import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactoryTests; -import org.springframework.boot.web.server.WebServerException; +import org.springframework.boot.web.server.PortInUseException; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -46,14 +46,15 @@ public class NettyReactiveWebServerFactoryTests } @Test - @Ignore public void exceptionIsThrownWhenPortIsAlreadyInUse() { AbstractReactiveWebServerFactory factory = getFactory(); factory.setPort(0); this.webServer = factory.getWebServer(new EchoHandler()); this.webServer.start(); factory.setPort(this.webServer.getPort()); - this.thrown.expect(WebServerException.class); + this.thrown.expect(PortInUseException.class); + this.thrown.expect( + Matchers.hasProperty("port", Matchers.equalTo(this.webServer.getPort()))); factory.getWebServer(new EchoHandler()).start(); }