Injecting Mockito mocks into a Spring bean

后端 未结 22 1489
庸人自扰
庸人自扰 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:17

    For the record, all my tests correctly work by just making the fixture lazy-initialized, e.g.:

     
    
    
    
    
        
    
    
    
        
    
    

    I suppose the rationale is the one Mattias explains here (at the bottom of the post), that a workaround is changing the order the beans are declared - lazy initialization is "sort of" having the fixture declared at the end.

提交回复
热议问题