Reputation: 410
I'm currently thinking about expanding unit tests for a server and a client application. Mockito is already in use and - I think - very well suited for the task at hand. However, as the Mockito documentation itself admits:
Mockito is not an dependency injection framework, don't expect [the @InjectMocks annotation] to inject a complex graph of objects be it mocks/spies or real objects.
The server-side of course already has CDI via annotations at some places, the client will probably be extended in some place to use CDI for JavaSE in the near future. There is/will be a wild mix of field- and constructor-injection + @postconstruct methods, which is already too complicated for Mockito. So I'm looking for something that will allow me to easily use CDI annotations to inject Mockito's mocks/spies/real objects where needed.
Can Mockito's functionality be expanded via plugins or something similar to enable a dependency resolution closer to what is specified by CDI (I don't think I need the full spec, but something closer to it)? Is there another library that integrates with Mockito and JUnit5 that does that?
Upvotes: 0
Views: 1756
Reputation: 3593
cdi-unit and ioc-unit have modules which support that. Just add
@Produces
@Mock
ClassName mockedObject;
and the framework will make it an injectable Bean.
Upvotes: 0
Reputation: 308
If you wish to mock EJB/CDI beans with OpenEJB, you can do it very easily: http://tomee.apache.org/master/examples/rest-applicationcomposer-mockito.html
Upvotes: 1
Reputation: 410
weld supports this out of the box, specifically by weld-junit. It supports both junit4 and junit5. In both cases one can define producer methods for the required injection points in which one can freely use mockito or powermock or any other mocking mechanism to create mocks which weld then injects into the test object.
Upvotes: 2