I have a general question, illustrated with a concrete example. How many would you recommend testing compound objects when all component objects have already been tested?
As a specific example, consider the NullTerminatedStringReader below. It reads a zero-terminated string from the byte buffer. To do this, use the Javas Charset decoder.
Of course I want to test my NullTerminatedStringReader. It should be able to read all types of strings, such as UTF8, UTF16, ASCII, etc.
Imagine that I wrote CharsetDecoder and verified that it will decrypt characters from all possible possible encodings. It is REALLY well tested and tried, and I have no doubt that it works. Now I am writing a NullTerminatedStringReader that uses a CharsetDecoder. In theory, I want NullTerminatedStringReader to be able to process all encoding strings that CharsetDecoder can decode. If I used TDD, I would like to manage my design, so I would write a lot of tests testing each encoding decoder for NullTerminatedStringReader:
... void testNullTerminatedStringReaderCanDecodeUTF8String() void testNullTerminatedStringReaderCanDecodeASCIIString() ...
But this seems superfluous, because in the test for CharsetDecoder I have all these tests:
... void testCharsetDecoderCanDecodeUTF8Char() void testCharsetDecoderCanDecodeASCIIChar() ...
I'm not sure what to do here, because without tests for the NullTerminatedStringReader, how can I manage this design to support all of these decoding? Am I testing at the wrong level for NullTerminatedStringReader? If I don't do tests, this also seems to be missing, because in theory I know that NullTerminatedStringReader uses CharsetDecoder, and I know that all he does is add characters to form a string, so there is not much that can go here not this way. If CharsetDecoder works, then NullTerminatedStringReader. But what if he did not use CharsetDecoder - I think it is more understandable to assume that there is no information about the implementation of NullTerminatedStringReader, and then write tests, however this leads to what seems like redundant tests. Dillema? You are betting.
class NullTerminatedStringReader { private Charset charset; public String read(ByteBuffer buffer) { StringBuilder sb = new StringBuilder(); while (true) { char charVal = readChar(buffer, charset.newDecoder());