Hi everybody, I've found my self in the following situation. I'm making a
socket server, with a custom protocol. I'm using ByteBuffer with
SocketChannel for the reading. I'm implementing a state pattern for every
step of the protocol. I've done few tests on each protocol state subclass,
but in the acceptance test, when I test the whole protocol, I've found,
debugging, that in the production code I missed a call to byteBuffer.flip(),
which put the limit to the current position, and the position to zero, so I
can read new contents.
My doubt is what kind of test would be the best to test this situation:
1) Mock ByteBuffer, and then test that the flip method was called. (this way
sounds confusing, I could mock everything, and make a verification per line
of production code... :S.. I don't know where the limits should be).
2) Use a real ByteBuffer, put something, and then test that what I putted,
was readed without problems.
I hope you can help me out, I'm new at TDD. Greetings
EDIT: I readed what I writed above, and I think that the best choice would
be choice #2. But this brings me to another problem. I've read somewhere
that unit tests should be independent about their collaborators classes. So,
I should not rely on that the flip method is working (Choise #2), I should
just make shore that the production code that I'm testing is calling the
flip method (Choise #1). Choise #1 would take me to make a mock and
verification for every production's line of code. Any help about this?
With choice #2, as long as the code you are relying on is tested elsewhere, it is fine. You miss the problem of testing the code using production code because you aren't testing that particular section of the code at the time. It's not pure "unit" testing but most things with sockets aren't going to be because you are testing that higher/integration level.