Mocking a class vs. mocking its interface

It may not make much difference in your case but the preferred approach is to mock interface, as normally if you follow TDD (Test Driven Development) then you could write your unit tests even before you write your implementation classes. Thus even if you did not have concrete class DataAccessImpl, you could still write unit tests using your interface DataAccess.

Moreover mocking frameworks have limitations in mocking classes, and some frameworks only mock interfaces by default.

Leave a Comment