SocketUtils was introduced in Spring Framework 4.0, primarily to assist in writing integration tests which start an external server on an available random port.

However, these utilities make no guarantee about the subsequent availability of a given port and are therefore unreliable.

Instead of using SocketUtils to find an available local port for a server, it is recommended that users rely on a server's ability to start on a random port that it selects or is assigned by the operating system. To interact with that server, the user should query the server for the port it is currently using.

Comment From: snicoll

This is a user-facing change that should be visible in the changelog so I've switched the issue type to enhancement.

Comment From: sbrannen

FYI: SocketUtils has been removed in Spring Framework 6.0 M3.

  • 28054

Comment From: chrylis

I know that a PR to remove SocketUtils has already been merged, but I will make a note requesting a revert of the removal. Despite the comment in the Javadoc to "rely on a server's ability to start on a random port", the assumption that the external dependency has such an ability is not well-founded; the DynamoDB Local server, in particular, does not have this feature, and I'm using findAvailableTcpPort for this purpose. The only suitable replacement will be to clone the code anyway.

As an alternative, would moving this to Spring Test be sensible?

Comment From: sbrannen

Hi @chrylis,

Thanks for the feedback.

As an alternative, would moving this to Spring Test be sensible?

We are considering introducing an alternative (with reduced scope) for testing scenarios only in spring-test. If so, it could potentially look similar to TestSocketUtils, though the final API might be different.

We will continue to deliberate this and listen to community feedback.

Comment From: sbrannen

Please note that the team plans to introduce TestSocketUtils in Spring Framework 5.3.18. See #28210 for details.