4 ms·
The very first example is about DI and advocates MacWire to do that in Scala: lazy val userDao = wire[UserDao] I come from Java and when I moved to Scala I wa
by misja111 10y ago
The very first example is about DI and advocates MacWire to do that in Scala:
lazy val userDao = wire[UserDao]
I come from Java and when I moved to Scala I wanted to use DI to mock dependencies in my unit tests. The logical choice for DI seemed to be MacWire, however it did not help me with the mocking at all.
In Java you have Mockito and annotations such as @InjectMocks that inject all your mocks into your SUT automagically. In Scala, regardless if I used MacWire or not, I had to override my SUT's implementation and its dependent fields by myself, or use constructors that take all the dependencies as arguments so I could override them in my tests.
Luckily this is much easier in Scala than in Java, but to me MacWire was a disappointment.
- tutanchamun 10y agoYou could also just use Google Guice (but you would loose compile time safety) like playframework. Maybe you like that better than MacWire?
- caoilte 10y agoEventually you learn not to need mocks. It's a long road though. An important first step is making your dependencies very very simple functions that are trivial to define on the fly.
- misja111 10y agoYes I agree, in Scala mocks are not as useful as in Java and the Scala language has some features that make it easier to work around them. However what I don't understand is what is so great about MacWire. Maybe I'm missing something but I always believed that the main advantage of a DI framework was that it facilitated mocking. If this is not needed in Scala, then what is so great about MacWire?
- caoilte 10y agoYou're right. The biggest thing that DI frameworks have historically done for me is make mock injection easier. There are other benefits, however. - Manual wiring generally has quite a lot of boiler plate (even in Scala, especially if you use free monads) - DI frameworks enforce a pattern of behaviour. This can be especially helpful when navigating a project. Having said that, whether these benefits are worth the cost of a DI framework is debatable. My current weapon of choice is FreeK...