concept mock in category android

This is an excerpt from Manning's book Android in Practice.
Unfortunately, isolating code units from each other can be surprisingly difficult to do. Mock objects can help (we’ll learn what mocks are and how to use them in section 13.3), but for classes to be properly testable using unit tests, you should think about this beforehand, ideally while writing the class. TDD can help by designing classes that are loosely coupled and therefore can easily be isolated in tests. Entire books can be written on how to achieve that, but let’s not digress too far here. Note that we’ll write a unit test for DealDetails in technique 75, so sit tight: a coding exercise is on its way.
First, we need to define a mock that’ll mimic the file output. We do that by inheriting from FileOutputStream and configuring it to write to STDOUT (by passing the FileDescriptor.out object to its constructor; note that it doesn’t matter what you pass here, as long as it’s a valid file descriptor, since we’re going to override write in the next step)
. We also override its write method to not write to a file, but to keep a record of the number of invocations in the itemsWritten field
, and to make sure that the data passed to this method is what we expect: a String-ified deal item
.
. We also override its write method to not write to a file, but to keep a record of the number of invocations in the itemsWritten field
, and to make sure that the data passed to this method is what we expect: a String-ified deal item
.