Recently I had to find a solution for mocking arbitrary-depth-level beans in Spring tests; when saying "arbitrary-depth-level beans" I mean a situation where, for example, I have the following Autowiring Chain:
Test Class -> ServiceA -> ConnectorFactoryBean(Singleton) -> ConnectorBean (Prototype) -> ConnnectionFactory(Singleton) -> Connection (Prototype).
During test execution, I'd like to mock the connection factory used by the ConnectorBean, thus opening a fixed-result connection instead of opening a real external data source connection which is not needed by my test, as I'm testing higher level functionality.
Had the object dependency graph been simpler, I could have just injected the mock into the service in the test itself, but that is quite possible, especially as prototype beans are tricky to follow.