0

Somebody explained it to me once that, In spring framework, we program to the interface rather than programming to concerete class, because it leads to tight coupling, and that's problematic in unit testing while doing the mocking.

I understand the tight coupling part based on the example here, but I don't understand how tight coupling is a problem with unit testing and mocking.

Just like the way we mock an interface, we can mock a concerte class. So, how tight coupleing hurts the unit testing?

Max
  • 9,100
  • 25
  • 72
  • 109
  • If that's exactly how they described it to you, and not you misinterpreting what they said, then they don't know what they're talking about. Use an interface when it makes sense, and don't when it doesn't make sense: there's no special reason why Spring is any different in this regard. Mockito doesn't care whether something is an interface or a class. – Michael Jun 14 '22 at 11:58
  • Tight coupleing adds complexity to unit testing. Loose coupling makes unit testing simple. Mocking can help with complex dependencies, but a better practice is no dependencies. – shanfeng Jun 15 '22 at 01:10

0 Answers0