### Question:
Magrit { void tryBind(int port) throws IOException { ServerSocket ss = null; try { ss = new ServerSocket(port); } finally { if (ss!= null && ss.isBound()) { ss.close(); } } } Magrit(); static void main(String[] args);  }### Answer:
@Test(expected=BindException.class) public void testTryBind() throws Exception { int port = 22222; ServerSocket ss = new ServerSocket(port); try { magrit.tryBind(port); } finally { ss.close(); } }
@Test public void testTryBind_releasePortAfterTry() throws Exception { int port = 22222; magrit.tryBind(port); try { magrit.tryBind(port); } catch (BindException e) { fail("Magrit.tryBind(int) didn't release the TCP port."); } }