Injecting Mockito mocks into a Spring bean

后端 未结 22 1187
庸人自扰
庸人自扰 2020-11-22 09:44

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the

22条回答
  •  醉话见心
    2020-11-22 10:00

    I found a similar answer as teabot to create a MockFactory that provides the mocks. I used the following example to create the mock factory (since the link to narkisr are dead): http://hg.randompage.org/java/src/407e78aa08a0/projects/bookmarking/backend/spring/src/test/java/org/randompage/bookmarking/backend/testUtils/MocksFactory.java

    
        
    
    

    This also helps to prevent that Spring wants to resolve the injections from the mocked bean.

提交回复
热议问题