Given this class:
public SomeClass implements SomeInterface {
@Autowired private RemoteService rService;
}
And given this unit test:
public SomeClassTest {
...
SomeClass sc = (SomeClass) ctx.getbean("someService");
..
}
I want to stub the ‘rService’ with some other object. I do not want to have to modify the SomeInterface method to expose any getters/setters since ‘rService’ only applies to just one implementation.
Besides just resetting the mock/stub of
RemoteServicethrough the setter injection within the test:(if the above does not work for you for some reason…) => you can create a “test-config.xml”, where the only bean you’d redefine is
remoteServicewith the same (real) bean name, but with a stub implementation, so it can override the real bean:where
RemoteServiceStubwould extend theRemoteService, so it can be autowired by type intoSomeClassThen in your test, inject SomeClass as you would normally do: