Skip to content
Advertisement

How to disable @PostConstruct in Spring during Test

Within a Spring Component I have a @PostConstruct statement. Similar to below:

JavaScript

During Unit tests I would not like to have the @PostConstruct function called, is there a way to telling Spring not to do post processing? Or is there a better Annotation for calling a initiation method on a class durning non-testing ?

Advertisement

Answer

Since you are not testing FileListService but a depending class, you can mock it for tests. Make a mock version in a separate test package which is scanned only by test context. Mark it with @Primary annotation so it takes precedence over production version.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement