The point about how singletones are handled in Spring is that there is no code in the singleton that restricts what it's called, it's just a POJO. Spring is responsible for ensuring that everyone receives the same instance. This means that if you want to write unit test, your test should not use Spring for it at all, the test can create a singleton instance, like any other POJO, as part of the test setup process, and the test code can connect mocks to its dependencies.
This is singleton code, providing it with a singleton, which makes testing difficult, and Spring is no longer a problem.
source
share