Mockito: Inject real objects into private @Autowired fields

后端 未结 5 2250
孤街浪徒
孤街浪徒 2020-11-29 17:56

I\'m using Mockito\'s @Mock and @InjectMocks annotations to inject dependencies into private fields which are annotated with Spring\'s @Autow

5条回答
  •  Happy的楠姐
    2020-11-29 18:22

    Mockito is not a DI framework and even DI frameworks encourage constructor injections over field injections.
    So you just declare a constructor to set dependencies of the class under test :

    @Mock
    private SomeService serviceMock;
    
    private Demo demo;
    
    /* ... */
    @BeforeEach
    public void beforeEach(){
       demo = new Demo(serviceMock);
    }
    

    Using Mockito spy for the general case is a terrible advise. It makes the test class brittle, not straight and error prone : What is really mocked ? What is really tested ?
    @InjectMocks and @Spy also hurts the overall design since it encourages bloated classes and mixed responsibilities in the classes.
    Please read the spy() javadoc before using that blindly (emphasis is not mine) :

    Creates a spy of the real object. The spy calls real methods unless they are stubbed. Real spies should be used carefully and occasionally, for example when dealing with legacy code.

    As usual you are going to read the partial mock warning: Object oriented programming tackles complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.

    However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.

提交回复
热议问题