Announcement Announcement Module
Collapse
No announcement yet.
New bean scopes and unit testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • New bean scopes and unit testing

    Hi folks,

    With the new functionality about bean scopes, something has broken in my application: unit testing. I was using AbstractDependencyInjectionSpringContextTests utility class to be able to inject my beans into JUnit tests, but I'm afraid this is no longer available when using something like scope="request" or scope="session".

    If you take a look at JavaDoc for AbstractDependencyInjectionSpringContextTests it says:

    <code>
    Really for integration testing, not unit testing. You should not normally use the Spring container for unit tests: simply populate your POJOs in plain JUnit tests!
    </code>

    But then, does it mean that I have to create, instantiate, populate all my beans in my JUnit tests? I want Spring to do that!!!!

    Any clues?

  • #2
    Explanation but no solution.

    From 3.4.3 of the spring reference manual

    The scopes that are described in the following paragraphs are only available if you are using a web-aware Spring ApplicationContext implementation (such as XmlWebApplicationContext). If you try using these next scopes with regular Spring IoC containers such as the XmlBeanFactory or ClassPathXmlApplicationContext, you will get an IllegalStateException complaining about an unknown bean scope.
    AbstractDependencyInjectionSpringContextTests implements a ConfigurableApplicationContext that is not web aware. I would also be intrested as to how other people Integration there session beans.

    Comment


    • #3
      In the meantime, I will use a copy of my Spring XML file for testing purposes, inside my test/resources folder. I will have to be aware of synchronizing it, but that will make the workaround until we find a feasible solution.

      Comment


      • #4
        Originally posted by e.medina.m
        Really for integration testing, not unit testing. You should not normally use the Spring container for unit tests: simply populate your POJOs in plain JUnit tests!
        But then, does it mean that I have to create, instantiate, populate all my beans in my JUnit tests? I want Spring to do that!!!!
        Just because a test is a JUnit test doesn't make it a unit test. What you are describing is an integration test. You are integrating your tested classs with other components.

        But as you pointed AbstractDependencyInjectionSpringContextTests can't be used with scoped beans. Sounds like a bug. You should consider posting a bug report at the Spring JIRA.

        Comment

        Working...
        X