### Question:
ProtoStorageClient { public Completable write(AbstractMessageLite messageLite) { return Completable.fromCallable( () -> { synchronized (this) { try (FileOutputStream output = application.openFileOutput(fileName, MODE_PRIVATE)) { output.write(messageLite.toByteArray()); return messageLite; } } }); } ProtoStorageClient(Application application, String fileName); Completable write(AbstractMessageLite messageLite); Maybe<T> read(Parser<T> parser);  }### Answer:
@Test public void write_noFailure_writesBytes() throws IOException { protoStorageClient.write(response).subscribe(); verify(fileOutputStream).write(response.toByteArray()); }
@Test public void write_noFailure_completes() { TestObserver<Void> subscriber = protoStorageClient.write(response).test(); subscriber.assertComplete(); }
@Test public void write_onFailure_notfiedError() throws IOException { when(application.openFileOutput(FILE_NAME, Context.MODE_PRIVATE)) .thenThrow(new FileNotFoundException()); TestObserver<Void> subscriber = protoStorageClient.write(response).test(); subscriber.assertError(FileNotFoundException.class); }
@Test public void write_noFailure_closesStream() throws IOException { protoStorageClient.write(response).subscribe(); verify(fileOutputStream).close(); }
@Test public void write_withFailureOpeningFile_doesNotCloseStream() throws IOException { when(application.openFileOutput(FILE_NAME, Context.MODE_PRIVATE)) .thenThrow(new FileNotFoundException()); protoStorageClient.write(response).subscribe(); verify(fileOutputStream, times(0)).close(); }
@Test public void write_withFailureWriting_closesStream() throws IOException { doThrow(new IOException()).when(fileOutputStream).write(response.toByteArray()); protoStorageClient.write(response).subscribe(); verify(fileOutputStream).close(); }