### Question:
CorePeripheral implements Peripheral { @Override public void disconnect() { synchronized (syncRoot) { if (bluetoothGatt != null) { bluetoothGatt.disconnect(); bluetoothGatt = null; } connectedRelay.accept(false); if (connectionStateSubject != null && connectionStateSubject.hasObservers()) { connectionStateSubject.onError(new ConnectionError(DISCONNECTION)); connectionStateSubject = null; } if (currentOperation != null && currentOperation.hasObservers()) { currentOperation.onError(new ConnectionError(DISCONNECTION)); currentOperation = null; } } } CorePeripheral(BluetoothDevice device, Context context); @Override Observable<ConnectableState> connect(); @Override Observable<Boolean> connected(); @Override Single<byte[]> read(UUID svc, UUID chr); @Override Completable write(UUID svc, UUID chr, byte[] data); @Override Completable registerNotification(UUID svc, UUID chr); @Override Completable registerNotification(UUID svc, UUID chr, @Nullable Preprocessor preprocessor); @Override Completable unregisterNotification(UUID svc, UUID chr); @Override Observable<byte[]> notification(UUID chr); @TargetApi(21) @Override Single<Integer> requestMtu(int mtu); @Override Single<Integer> readRssi(); @Override int getMaxWriteLength(); @Override void disconnect();  }### Answer:
@Test public void disconnect() { connect(); corePeripheral.disconnect(); connectTestObserver.assertError( throwable -> { ConnectionError error = (ConnectionError) throwable; return error != null && error.getCode() == DISCONNECTION; }); }
@Test public void connect_gattCallback_nonZeroStatus() { prepareConnect(false); bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, 99, BluetoothGatt.STATE_CONNECTED); connectTestObserver.assertError( throwable -> { ConnectionError error = (ConnectionError) throwable; if (error != null && error.getCode() == CONNECT_FAILED && error.getCause() != null && error.getCause() instanceof PeripheralError) { PeripheralError peripheralError = (PeripheralError) error.getCause(); return peripheralError.getErrorStatus() == 99; } return false; }); verify(bluetoothGatt).disconnect(); }
@Test public void connect_serviceDiscoveryGattFailed() { prepareConnect(false); bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, 0, BluetoothGatt.STATE_CONNECTED); connectTestObserver.assertError( throwable -> { ConnectionError error = (ConnectionError) throwable; if (error != null && error.getCode() == CONNECT_FAILED && error.getCause() != null && error.getCause() instanceof PeripheralError) { PeripheralError peripheralError = (PeripheralError) error.getCause(); return peripheralError.getCode() == SERVICE_DISCOVERY_FAILED && peripheralError.getErrorStatus() == ERROR_STATUS_CALL_FAILED; } return false; }); verify(bluetoothGatt).disconnect(); }
@Test public void connect_serviceDiscovery_gattCallback_nonZeroStatus() { prepareConnect(true); bluetoothGattCallback.onConnectionStateChange(bluetoothGatt, 0, BluetoothGatt.STATE_CONNECTED); verify(bluetoothGatt).discoverServices(); bluetoothGattCallback.onServicesDiscovered(bluetoothGatt, 99); connectTestObserver.assertError( throwable -> { ConnectionError error = (ConnectionError) throwable; if (error != null && error.getCode() == CONNECT_FAILED && error.getCause() != null && error.getCause() instanceof PeripheralError) { PeripheralError peripheralError = (PeripheralError) error.getCause(); return peripheralError.getCode() == SERVICE_DISCOVERY_FAILED && peripheralError.getErrorStatus() == 99; } return false; }); verify(bluetoothGatt).disconnect(); }