BluetoothSocketIoThread extends Thread { public boolean write(byte[] bytes) { boolean wasSuccessful = false; if (mOutputStream != null) { try { mOutputStream.write(bytes); wasSuccessful = true; } catch (IOException e) { if (!mIsShuttingDown) { Log.e(TAG, "write: Failed to write to output stream: " + e.getMessage(), e); } } } else { Log.e(TAG, "write: No output stream!"); } if (wasSuccessful) { mListener.onBytesWritten(bytes, bytes.length, this); } return wasSuccessful; } BluetoothSocketIoThread(BluetoothSocket socket, Listener listener); BluetoothSocket getSocket(); @Nullable PeerProperties getPeerProperties(); void setPeerProperties(PeerProperties peerProperties); void setExitThreadAfterRead(boolean exit); int getBufferSize(); void setBufferSize(int bufferSizeInBytes); @Override void run(); boolean write(byte[] bytes); synchronized void close(boolean closeStreams, boolean closeSocket);  }
@Test public void testWrite() throws Exception { String someText = "some text"; assertThat("should return true if written properly", mBluetoothSocketIoThread.write(someText.getBytes()), is(true)); verify(mMockListener, times(1)).onBytesWritten(any(byte[].class), anyInt(), any(BluetoothSocketIoThread.class)); doThrow(IOException.class).when(mMockOutputStream).write(any(byte[].class)); reset(mMockListener); assertThat("should return false on failure", mBluetoothSocketIoThread.write(someText.getBytes()), is(false)); verify(mMockListener, never()).onBytesWritten(any(byte[].class), anyInt(), any(BluetoothSocketIoThread.class)); when(mMockBluetoothSocket.getOutputStream()).thenReturn(null); assertThat("should return false when no outputStream", mBluetoothSocketIoThread.write(someText.getBytes()), is(false)); verify(mMockListener, never()).onBytesWritten(any(byte[].class), anyInt(), any(BluetoothSocketIoThread.class)); }