Announcement Announcement Module
No announcement yet.
How to unit test using spring beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to unit test using spring beans


    I wanted to make some unit tests of my classes that use the spring support classes but I got stuck. The documentation says that org.springframework.orm.hibernate.LocalSessionFact oryBean behaves as a SessionFactory, but there is no straightforward way that I can see to simulate that behaviour.

    So when I am creating the beans that need a sessionFactory, I cannot assign that bean.

    Does this make sense?

    Thanks in advance.

  • #2
    Check out the interfaces it implements (FactoryBean) and how exactly that is used by Spring.


    • #3
      Thanks for that.

      Any hints on how to make spring parse the configuration file and set the properties of the objects I create? Or even better, how to create the Spring management from the configuration file and just request the components I want rather than rely on IoC?


      • #4
        You can ask Spring to do autowiring (so it can automatically set the properties you declare if such a thing is possible (i.e. no beans with the same type).
        You can request the components yourself by programatically getting the applicationContext and retrieving beans from there; however, you loose the whole purpose of IoC.


        • #5
          It's the only way to write proper unit tests.

          Is there any example of what you speak of?