Reputation: 476
I have the following scenario and looking for the easiest way to address it: I have a spring bean that is responsible for reporting errors on prod environment. On dev environments I'd like to replace it with a stub.
Ideally, what I'm looking for is something like @ConditionalOnPropertyOrMock annotation similar to spring's @ConditionalOnProperty. Is something like that doable with spring itself or some other libraries?
I went through few dozens of semi-similar questions about tests, but my case is not for tests. I believe it's possible to make it with a single annotation, therefore I don't like idea of adding a configuration class for mocked beans and initializing them one by one using Mockito/CGLIB/Proxy.
Upd. Looks like there's no a ready-to-use solution at the moment. It seems that the way to do it how I want would be to implement a Condition (SpringBootCondition)
and BeanFactoryPostProcessor (BeanDefinitionRegistryPostProcessor)
in one class. Then reject beans as Condition and introduce mocks as FactoryPostProcessor.
The easy solution at the moment would be to create beans in configuration class and make if/else there.
Upvotes: 0
Views: 323
Reputation: 476
Putting that as an answer, but still, looking for a possibly better solution.
Created a small utility that does what I need. https://github.com/astafev/spring-mock-bean
Upvotes: 0