Mockito + Spring + @PostConstruct, mock initialization error, why is @PostConstruct called?

前端 未结 1 748
悲哀的现实
悲哀的现实 2021-02-10 11:21

I have a set up like:

Bean class:

private final Map configCache = new HashMap<>();
@PostConstruct
private v         


        
相关标签:
1条回答
  • 2021-02-10 11:38

    Mockito isn't calling @PostConstruct -- Spring is. You say that in your test you use @Autowired, which is not a Mockito annotation.

    If you meant to use @Mock, you'll find that Mockito won't call your @PostConstruct method.

    In other words, write your test class like this:

    @Mock Bean myBean;
    
    @Before
    public void before() {
        MockitoAnnotations.initMocks();
    }
    
    0 讨论(0)
提交回复
热议问题