I have a set up like:
Bean class:
private final Map<String, String> configCache = new HashMap<>();
@PostConstruct
private void fillCache() { (...) configCache.clear();}
TestConfig class:
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
@Primary
public Bean beanMock() {
return Mockito.mock(Bean.class);
}
Test class: which @Autowires
the bean.
It seems when Mockito is creating the mock in TestConfig, it calls @PostConstruct which in turn seems to be called before the map field is initialized so it throws an exception.
My question is:
EDIT: Apparently the call is done after the instantiation just before Spring retrns the bean from a Config's @Bean method
Methods marked with the @PostConstruct will be invoked after the bean has been created, dependencies have been injected, all managed properties are set, and before the bean is actually set into scope.
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection.
When we annotate a method in Spring Bean with @PostConstruct annotation, it gets executed after the spring bean is initialized. We can have only one method annotated with @PostConstruct annotation. This annotation is part of Common Annotations API and it's part of JDK module javax.
We can replace @PostConstruct with BeanFactoryPostProcessor and PriorityOrdered interface. The first one defines an action that ought to be executed after the object's instantiation. The second interface tells the Spring the order of the component's initialization.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With