This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TDD: What would be a correct test in this situation?

 
Gian Franco Zabarino
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

PS: BTW, I'm using Mockito for mocks

Gian Franco Zabarino


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?
Greetings

Gian Franco Zabarino
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gian Franco Zabarino
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, are you saying that if I have an acceptance/higher lever test that fails when that line of code is missing, I don't have to test that at the unit level?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While that is still TDD, that's not really what I had in mind. I meant that you have a unit test that fails if that line is missing, it just happens to call existing code to do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic