• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic