frankdejonge / testing-without-mocking-frameworks

29 stars 0 forks source link

Question: Tests coupled to the implementation? White-box tests? Huge amount of work for manual dependency injection setup? #2

Open matheusgontijo opened 9 months ago

matheusgontijo commented 9 months ago

@frankdejonge thank you so much for your great content as well as for sharing with the community 🙏🙏🙏 Greatly appreciated! 🙏🙏🙏

I have a few questions about your testing approach without mocks. I would be more than happy if you could find some time to help me clarify them, if that's possible by any chance.

👉👉 1 - Let's say that we are building an e-commerce project, for example. Usually applications like that have lots of layers to be tested, in contrast to libraries.

MyCustomService001 (concrete implementation)
    └── MyCustomService002 (concrete implementation)
        └── MyCustomService003 (concrete implementation)
            └── MyCustomService004 (concrete implementation)
                └── MyCustomService005 (concrete implementation)
                    └── MyCustomService006 (concrete implementation)
                        └── *** MyCustomRepositoryInterface *** (interface)  👈👈👈

Let's say that all MyCustomServiceXXX classes don't touch external resources, like database/http/filesystem and etc... The only external hit is from MyCustomRepositoryInterface. That means I need to pass MyCustomRepositoryInterface with a concrete fake implementation MyCustomRepositoryFAKE all the way down from MyCustomService001 until MyCustomService006?

I mean, that way my test is going to touch all classes MyCustomService001 until MyCustomService006, correct?

👉👉 2 - If yes, then I would need to create aaaaaalllll dependencies manually, correct? Kinda of perform my own dependency injection for each test. For many layers, this work seems take a good amount of work... doesn't it? Especially during refactoring.

Is not there a tricky DI manager strategy to make it easier to several layers? It would be super helpful, in case you know any to share with us 👍👍👍

👉👉 3 - This once was sold me like a black-box strategy, but I was thinking to other day... this approach is actually white-box! Is not it or I'm out of my mind?

Reason: you need to know very well the implementation to make sure you create fake "ports/adapters". Also, if somebody in my team refactors the MyCustomService006 to use another two repositories, then one needs to also refactor my test... So, our test is coupled to the internal implementation... is it correct to assert that or I'm missing something?

Once again, thank you so much for your time and greatly appreciate your work 🙏🙏🙏

frankdejonge commented 9 months ago

Hi @matheusgontijo, hope you're doing well. It's pretty hard to make a concrete out of this, but I'll do my best. There are certainly some things I have to share about this.

I hope this helps :) and thank you for your kind words regarding the original post. I'm glad to see it keeps on being relevant.

matheusgontijo commented 9 months ago

Thank you so much for your thoughts as well as for quick reply @frankdejonge. No enough words to thank you 🙏🙏🙏