Unit testing: Call @PostConstruct after defining mocked behaviour

前端 未结 2 1356
没有蜡笔的小新
没有蜡笔的小新 2021-01-08 00:09

I have two classes:

public MyService {
    @Autowired
    private MyDao myDao;     
    private List list; 

    @PostConstruct
    private void         


        
2条回答
  •  猫巷女王i
    2021-01-08 00:56

    MyDao sounds like it is an abstraction of an external system. Generally external systems shouldn't be called in @PostConstruct methods. Instead have your getItems() called by another method in MyService.

    Mockito injections will take place after the Spring initiation at which point the mock isn't working as you see. You cannot delay the @PostConstruct. To beat this and have the load run automatically have MyService implement SmartLifecycle and call getItems() in start().

提交回复
热议问题