@Test
public void testWithRandomOpenPort() throws Exception {

    DelegateExecution execution = mock(DelegateExecution.class);
    when(execution.getVariable(eq(IsMachinePortOpen.MACHINE)))
        .thenReturn(Machine.builder().localhost().createMachine());
    ProcessVariablesCollector collector = new ProcessVariablesCollector();
    collector.install(execution);
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(0);
        JavaDelegate delegate = new IsMachinePortOpen(RESULT, socket.getLocalPort());
        delegate.execute(execution);
        assertThat((Boolean) collector.getVariable(RESULT)).isTrue();
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}