Source:
<code>public final void testDiscoverDevicesNoDevices() throws Exception { XBeeProtocol protocol = XBeeProtocol.ZIGBEE; PowerMockito.when(deviceMock.getXBeeProtocol()).thenReturn(protocol); DiscoveryListener listener = new DiscoveryListener(); listeners.add(listener); long start = System.currentTimeMillis(); nd.startDiscoveryProcess(listeners); long endBlock = System.currentTimeMillis(); assertThat("The 'discoverDevices' should not block", endBlock - start < TIMEOUT, is(equalTo(true))); assertThat("The 'discoverDevices' should be running", listener.isFinished(), is(equalTo(false))); <START> while(!listener.isFinished()) { <END> Thread.sleep(100); } long end = System.currentTimeMillis(); assertThat("The 'discoverDevices' process should finish after " + TIMEOUT + " ms", end - start < TIMEOUT + 150 , is(equalTo(true))); assertThat("The discovered devices list should be empty", listener.getDiscoveredDevices().size(), is(equalTo(ndAnswers.size()))); PowerMockito.verifyPrivate(nd, Mockito.times(1)).invoke(SEND_NODE_DISCOVERY_COMMAND_METHOD, Mockito.anyString()); Mockito.verify(deviceMock, Mockito.times(1)).addPacketListener(packetListener); Mockito.verify(deviceMock, Mockito.times(1)).removePacketListener(packetListener); PowerMockito.verifyPrivate(nd, Mockito.times(1)).invoke(DISCOVER_DEVICES_API_METHOD, listeners, null); List<RemoteXBeeDevice> ndInternalDeviceList = Whitebox.<List<RemoteXBeeDevice>> getInternalState(nd, DEVICE_LIST); assertThat("Internal Node Discovery list must be empty", ndInternalDeviceList.size(), is(equalTo(0))); Mockito.verify(networkMock, Mockito.never()).addRemoteDevices(Mockito.anyListOf(RemoteXBeeDevice.class)); Mockito.verify(networkMock, Mockito.never()).addRemoteDevice(Mockito.any(RemoteXBeeDevice.class)); }</code><technical_language>50ms for sleep: java while (!listener.isFinished()) { Thread.sleep(50); }</technical_language>

Target:
public final void testDiscoverDevicesNoDevices() throws Exception { XBeeProtocol protocol = XBeeProtocol.ZIGBEE; PowerMockito.when(deviceMock.getXBeeProtocol()).thenReturn(protocol); DiscoveryListener listener = new DiscoveryListener(); listeners.add(listener); long start = System.currentTimeMillis(); nd.startDiscoveryProcess(listeners); long endBlock = System.currentTimeMillis(); assertThat("The 'discoverDevices' should not block", endBlock - start < TIMEOUT, is(equalTo(true))); assertThat("The 'discoverDevices' should be running", listener.isFinished(), is(equalTo(false))); while(!listener.isFinished()) { Thread.sleep(50); } long end = System.currentTimeMillis(); assertThat("The 'discoverDevices' process should finish after " + TIMEOUT + " ms", end - start < TIMEOUT + 150 , is(equalTo(true))); assertThat("The discovered devices list should be empty", listener.getDiscoveredDevices().size(), is(equalTo(ndAnswers.size()))); PowerMockito.verifyPrivate(nd, Mockito.times(1)).invoke(SEND_NODE_DISCOVERY_COMMAND_METHOD, Mockito.anyString()); Mockito.verify(deviceMock, Mockito.times(1)).addPacketListener(packetListener); Mockito.verify(deviceMock, Mockito.times(1)).removePacketListener(packetListener); PowerMockito.verifyPrivate(nd, Mockito.times(1)).invoke(DISCOVER_DEVICES_API_METHOD, listeners, null); List<RemoteXBeeDevice> ndInternalDeviceList = Whitebox.<List<RemoteXBeeDevice>> getInternalState(nd, DEVICE_LIST); assertThat("Internal Node Discovery list must be empty", ndInternalDeviceList.size(), is(equalTo(0))); Mockito.verify(networkMock, Mockito.never()).addRemoteDevices(Mockito.anyListOf(RemoteXBeeDevice.class)); Mockito.verify(networkMock, Mockito.never()).addRemoteDevice(Mockito.any(RemoteXBeeDevice.class)); }