permaculture playing cards*
The moose likes Testing and the fly likes TDD: What would be a correct test in this situation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "TDD: What would be a correct test in this situation?" Watch "TDD: What would be a correct test in this situation?" New topic
Author

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

Gian Franco Zabarino
Greenhorn

Joined: Jun 08, 2011
Posts: 2
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gian Franco Zabarino
Greenhorn

Joined: Jun 08, 2011
Posts: 2
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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.
 
Consider Paul's rocket mass heater.
 
subject: TDD: What would be a correct test in this situation?
 
Similar Threads
Private methods in TDD
Unit Testing is not LEAN
Q on post increment: JLS explanation
Q 4 Mr. Hunt & Mr Thomas (5): unit test patterns
fitNesse vs junit