• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of interfaces for unit testing

 
Aaron Shaw
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm thinking that any class of reasonable complexity should be 'interfaced out', so that it can be completely removed as a dependancy in unit tests, and then tested itself in as much isolation as possible.

Eg, I have a session bean which does some stuff. It uses a validation class to do a load of validation. Would it make sense to make the validator implement an interface so that i can test it seperately, significantly simplifying the unit tests for the session bean?

The reason I ask is that thinking like this could produce alot of interfaces, but I think that the extra overhead of maintaining these interfaces will be massively outweighed by the benefits to unit testing.

I'd appreciate any comments
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron,
We have a forum for testing. I'll move this there for you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron,
I tend to use the rule of thumb of "mock out anything that accesses an external resource or is hard to test."

By this logic, I would mock out a call to a data access object or processor, but not a validator. Unless it's really hard to pass in valid input, mocking it doesn't add any extra value for me. Regardless, I would test the validator separately. That's separate from whether to use an interface though. It's more of a statement that I'd rather test the happy path for the validator without mocks.

I'm not a unit testing purist though. I want my unit tests to test as much as possible without going overboard.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
I tend to use the rule of thumb of "mock out anything that accesses an external resource or is hard to test."

By this logic, I would mock out a call to a data access object or processor, but not a validator. Unless it's really hard to pass in valid input, mocking it doesn't add any extra value for me.


Another reason for me would be to make the tests less brittle. That is, if I have to write a lot of tests for the session bean that need to pass in valid input, all those tests might break when the validation rules change - although the session bean still works as expected. Using a mock validator for the session bean tests would be one solution to that problem.
 
Aaron Shaw
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers guys.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic