Results 1 to 4 of 4

Thread: Load only dependencies of requested bean?

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    107

    Default Load only dependencies of requested bean?

    Hi all,

    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?

    Thanks.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2007
    Posts
    107

    Default

    Thank you.

    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?

  4. #4
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Check 'default-lazy-init' attribute of the root <beans> element. Corresponding reference section - 3.3.4. Lazily-instantiated beans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •