wood burning stoves 2.0*
The moose likes Testing and the fly likes Use of interfaces for unit testing 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 "Use of interfaces for unit testing" Watch "Use of interfaces for unit testing" New topic
Author

Use of interfaces for unit testing

Aaron Shaw
Greenhorn

Joined: Jan 19, 2007
Posts: 9
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Aaron,
We have a forum for testing. I'll move this there for you.


[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
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Aaron Shaw
Greenhorn

Joined: Jan 19, 2007
Posts: 9
Cheers guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of interfaces for unit testing
 
Similar Threads
JUnit Testing - for a complete Java Project.
Multiple inheritance, static functions, and code reuse
UnitTesting
Unit Testing with Groovy?
Simple Spring Junit -