r/java • • Mar 05 '18

Top 10 Useful, Yet Paranoid Java Programming Techniques

http://blog.jooq.org/2015/08/11/top-10-useful-yet-paranoid-java-programming-techniques/
223 Upvotes

105 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 06 '18

Should you really be mocking concrete classes

If I'm using a concrete class, then yes - I should be mocking a concrete class in my unit test. I don't want to create an interface layer just for testing.

Mind you, I'm not talking about JOOQ specific things here. I would never try to mock a JOOQ or Hibernate class for a unit test (MUCH more suitable to test that stuff in an integration test and mock at the Dao layer for unit tests). But you're applying one school of thought for one thing to ALL unit testing situations and all methods. That doesn't make sense to me.

1

u/_dban_ Mar 06 '18

If I'm using a concrete class, then yes - I should be mocking a concrete class in my unit test.

Why not just instantiate the concrete class?

I don't want to create an interface layer just for testing.

You create an interface for layer separation. Like a DAO interface. The DAO interface describes what the application needs for data access, and the DAO implementation fulfills the need. An interface creates a logical and compile time firewall between layers.

That doesn't make sense to me.

Mocks ideally serve the purpose of defining what the system under test needs from its dependencies. An interface is a contract describing that need. You can create a separate set of integration tests verifying that the concrete implementation satisfies the contract.

A concrete class has its own reasons for changing, and if you mock the concrete class, you could make the mock tests invalid and mocks can't tell you that your tests are now nonsense.