We often design our code so that one procedure comprises many classes, often grouped by layers. For example, in web dev, that could be controllers, models, repositories, etc:
![](https://bartoszkrajka.com/wp-content/uploads/2023/11/pic1.drawio-3.png)
Layers are good. Often at least two layers are desirable for maintainability: domain and low-level. If you’re like me, you like 2-5 layers but not too many.
Now how do you test such code? It comes naturally to want to test each component separately and mock out the collaborators from other layers. I believe it’s because unit tests are said to be run “in isolation”:
![](https://bartoszkrajka.com/wp-content/uploads/2023/11/pic2.drawio-1.png)
My advice – don’t automatically do this. Aim for this instead:
![](https://bartoszkrajka.com/wp-content/uploads/2023/11/pic3.drawio.png)
Recent Comments