Monday, February 17, 2014

Well, hello @Mock - we meet again

tl;dr Since I embraced Domain-Driven Design, I didn't have to use any mock.

When in 2011, during Java Developers Day in Kraków, Greg Young stated, that he does not use mocks, I couldn't believe him. I mean, come on! How do you test your code that is dependent on other parts of the code? I was at the very begining of my journey with Domain-Driven Design back then...

Couple of days ago, I had to implement something in old part of the system. This part was developed like most systems were couple of years ago (and some are still) - with Anemic Entities and stateless, so called "Services". I didn't want to refactor this whole spaghetti around that place, so I decided to go with this anemic approach and "just be good enough".

Since I am a big fan of Test-Driven Development (where it is valuable), I decided to write some test for this service that I was about to touch. Suprisingly, there was a test class for this service, and there were even some not that bad unit tests there. Woohoo, lucky me!

I wrote first unit test, run - red of course - so I implemented some simple solution - green - I added another test - red - another simple solution - green - I refactored a little - green. Uncle Bob would be proud!

Then, the time has come to write some more complex case. I realized, that I need some additional service to be injected into mine. It was clear to me, that I needed to mock that service in test, so I wrote
@Mock
private AdditionalService additionalService;
and it felt weird... :/

I realised, that I haven't use mocks for a long time. I just didn't have to.