• 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

Use of interfaces for unit testing

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
Aaron,
We have a forum for testing. I'll move this there for you.
 
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
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers guys.
reply
    Bookmark Topic Watch Topic
  • New Topic