Oct 3rd, 2008, 04:05 AM
Load only dependencies of requested bean?
Imagine I have a context file with a lot of controller beans.
I want to run a single controller test.
When I run this test I only want the beans dependencies of that controller to be loaded by Spring and nothing else. For example I don't want all beans and all their dependencies in context file to be loaded.
What ways are there to achieve this? Is lazy loading sufficient? What alternatives are there architecturally?
Oct 3rd, 2008, 04:13 AM
It depends on what you want to test.
If it is testing ONLY the controller it is a Unit test and a unit test should contain preferably NO environmental/infrastructure dependencies. You would simply instantiate your controller, stub or mock the dependencies and run the method you want to run.
If it is an integration test you should have an environment/infrastructure (maybe scaled down version) and loading all your beans shouldn't be an issue. If you use Spring Test Support classes the context is loaded once only and reused after that.
Oct 3rd, 2008, 04:25 AM
Yeah I agree. Spring test support is being used for the controller integration tests.
I'm trying to do test a single controller without mocking and by loading only the environmental/infrastructural dependencies that that specific controller needs and nothing else. So I guess I want to load a full cross section of what's necessary to that one controller which is being tested. The reason it is not mocking is because it is doing integration testing.
I've already thought about context-caching, lazy-loading and modularised context files and realise that context caching is performed by Spring. However is there any way to ensure that beans and their dependencies are loaded on demand rather than loading everything and caching it for duration of tests running?
Oct 3rd, 2008, 05:33 AM
Check 'default-lazy-init' attribute of the root <beans> element. Corresponding reference section - 3.3.4. Lazily-instantiated beans