TcpPortCheck { public boolean checkOpen(){ try (Socket socket = new Socket(host, port)){ return true; } catch (IOException e) { return false; } } TcpPortCheck(String host, int port); boolean checkOpen();  }
@Test public void testCheck() throws Exception{ int port = randomPort(); Assert.assertFalse(new TcpPortCheck("localhost", port).checkOpen()); Server server = startEchoServer(port); Assert.assertTrue(new TcpPortCheck("localhost", port).checkOpen()); sleep(100); Assert.assertEquals(0, server.getConnected()); }