When I first started using Mockito to mock out objects in java tests I almost always used the @InjectMocks annotation to construct the CUT (Class Under Test). But after using it for awhile and noticing some downsides I've since stopped using it altogether.
Here is a list of reasons why:
- Less magical
- There are some occasions where I need to be in control of what gets injected so constructing the CUT manually keeps all the tests consistent
- In some tests, I don’t end up verifying some mocks are invoked because it’s internal stuff. When this happens Idea flags them as unused even though they are technically necessary to construct a valid CUT.
- I think it can sometimes hide a class that has too much responsibility. If you have a large construction line that should be a clue your class is doing too much.
- If you use it it would be real easy to forget to remove the mock in the test