Concrete class mocks as default

138 views
Skip to first unread message

Wolfgang Schmiesing

unread,
Mar 19, 2013, 2:59:14 PM3/19/13
to juk...@googlegroups.com
Hi,

As far as I understood Jukito automatically mocks interfaces and abstract classes annotated with @Inject in the class-under-test.
The only way I've been able to mock concrete class dependencies is by using bindMock(ConcreteClass.class) in the Jukito module.

Now my question: Is there any way to change this default behaviour to automatically mock any dependency injected into the class-under-test?
It would be extremely useful to be able to configure this e.g. at the module or runner level instead of manually mocking out all concrete classes.

Kind regards,

Wolfgang

Christian Goudreau

unread,
Mar 19, 2013, 3:27:07 PM3/19/13
to Jukito
You can use forceMock as well for ConcreteClass, but I'm not sure weither or not it is a good idea to mock concrete classes. What do you think @PhilBeaudoin?



--
You received this message because you are subscribed to the Google Groups "Jukito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jukito+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Christian Goudreau

Philippe Beaudoin

unread,
Mar 19, 2013, 9:51:24 PM3/19/13
to juk...@googlegroups.com

Usually, the need to mock a concrete class is an indication of a design problem. If the dependency is complex enough to be mocked then you should abstract it in an interface.

As such there is no way to automatically mock all dependencies of a class. I would not necessarily be opposed to a proposal in that direction if you have one.

ForceMock will mock any concrete class deriving from a specific type. If you have a market interface for the classes you want to mock then you could use ForceMock but it's probably not a very clean solution.

Wolfgang Schmiesing

unread,
Mar 21, 2013, 6:26:48 AM3/21/13
to juk...@googlegroups.com
Following the KISS principle we only use interfaces if necessary, e.g. when crossing boundaries to DAO EJB layer or if there is more than one possible implementation.
The need for concrete classes is based on the fact that in a unit test only the system-under-test should be tested, everything else should be mocked out regardless of being an interface or a concrete class.

While I can see your point, it would be great if you can give me some pointers where the mocking is done in Jukito. Would this be in "JukitoModule.bindIfConcrete()" ?

Christian Goudreau

unread,
Mar 24, 2013, 11:53:38 AM3/24/13
to Jukito
or if there is more than one possible implementation.
You have more than one possible implementation when you thing about test environments vs production environments. Injection through abstraction is there for that kind of purpose as well. While I agree that you should start with KISS, it still nice to be able to hid the real implementation behind an abstraction for that kind of purpose.
Reply all
Reply to author
Forward
0 new messages