The ServerSocket
instance should be available via factory, which (factory) is passed as a dependency to your class:
// Passing factory dependency via constructor injection
public PortChecker(IServerSocketFactory socketFactory)
{
this.socketFactory = socketFactory;
}
// ...
ServerSocket ss = this.socketFactory.GetServerSocket(port.getPortNumber());
ss.getLocalPort();
ss.close();
Then, in your unit test you can mock socketFactory
to return fake server socket and as a result "disconnect" it from any real world systems.
Note that ServerSocket
might also need to be abstraction (say, represented by interface/base class) so it can be mocked too.