ServerConnection implements PayloadHandling { public void connectTo(String code, ConnectCallback callback) { if (connectionsClient == null) { Log.e(TAG, "connectionsClient is null, cannot discover."); return; } String searchedClientName = null; for (String clientName : clients) { if (clientName.endsWith(code)) { searchedClientName = clientName; break; } } String endpoint = clientNamesToIDs.get(searchedClientName); if (endpoint != null && searchedClientName != null) { clientID = endpoint; connectionsClient.requestConnection(searchedClientName, endpoint, buildConnectionLifecycleCallback(callback)); } else { callback.failure("Did not find endpoint"); } } private  ServerConnection(); static ServerConnection getInstance(); void init(Context ctx); @Override void addHandle(int type, HandlePayload handle); @Override HandlePayload getHandle(int type); @Override void handlePayload(Payload payload); @Override void handlePayloadUpdate(PayloadTransferUpdate update); void discover(ConnectCallback callback, EndpointDiscoveryCallback discoveryCallback); List<String> getClients(); String getClientID(); void discoverConnect(String code, ConnectCallback callback); void connectTo(String code, ConnectCallback callback); void abort(); PipedOutputStream sendStream(); EndpointDiscoveryCallback buildEndpointDiscoveryCallback(String name, ConnectCallback callback); boolean isConnected();  }
@Test public void connectTo() { ServerConnection connection = ServerConnection.getInstance(); Whitebox.setInternalState(connection, "connectionsClient", mockClient); ConnectCallback callback = new ConnectCallback() { @Override public void success(String message) { } @Override public void failure(String message) { fail("Failed to connect"); } }; connection.discover(callback, connection.buildEndpointDiscoveryCallback("", callback)); connection.connectTo("me", callback); assertEquals("test", connection.getClientID()); }