Is it discouraged to use @Spy and @InjectMocks on the same field?

前端 未结 2 1282
孤独总比滥情好
孤独总比滥情好 2020-11-29 06:20

In the project I\'m working on right now, I often see the @Spy and @InjectMocks used together on a field. I have never seen it this way in any tuto

2条回答
  •  醉话见心
    2020-11-29 06:52

    Each annotation has different purposes and they don't step on each other clearly as long as you need to use partial mocks. (a.k.a. stubbing related method(s) that has been already tested and/or trusted)

    For example, you have a class to test, which has dependency injection(s) that doesn't have to be real so you want to @InjectMocks. Besides, the method you are testing calls another method inside, which was already tested somewhere, or it calls external reference which was also tested most likely independently. So, you don't want to be bothered to test same method(s) more than once, and your test code should not be impacted by the out-of-scope implementation change anytime in the future.

    Only @Mock & @Spy, or @Mock & @InjectMocks pairs make no sense.

提交回复
热议问题