Announcement Announcement Module
Collapse
No announcement yet.
newbie: How to use Spring created app context with JUnit/Hib Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie: How to use Spring created app context with JUnit/Hib

    All,

    I'm new to Spring/Hibernate and am currently trying to setup unit tests
    for some of my application's persistence related use cases.

    My application is a standalone (much like the rich client) and I simply
    use spring to wire up all the various beans that in the past used to be
    Singletons (ugly, I know).

    My problem is that a lot of the new code relies on this one Singleton, the
    Application context to provide it all the necessary infrastructure objects
    like DAOs, factories, etc. This means that I need to have the application
    context fully setup before I can start a single test case! Is there another
    way around it? Do I have to manually create the context with some
    default/test versions of these infrastructure objects?

    What are the common methods people use to test Hibernate code in
    applications using Spring?

  • #2
    This means that I need to have the application
    context fully setup before I can start a single test case! Is there another
    way around it? Do I have to manually create the context with some
    default/test versions of these infrastructure objects?
    You can produce different context files and extend them. Check the import statements for example.

    Also most of the objects (beside DAOs) can be set up programmatical. Meaning creating and injecting the dependency yourself, instead using Spring to do so. That eases testing also.

    Never the less take a look at the Spring distribution / CVS. There is a toplevel directory called mock. In there you find a package called org.springframework.test. Check out those files. These are test-cases for testing DAOs and other things. Check out how they work and maybe you can use them.

    I for myself use a context for testing DAO's and integration tests / acceptance tests. The rest is tested using mocks (jMock). The context is loaded once and held static until an exception is thrown on tear down / start up or during a test. If the exceptions happens during the context loading, all tests are ended.


    Cheers,

    Martin (Kersten)

    Comment


    • #3
      What about performance implications when using the context? Startup
      time is not insignificant when constructing the application context, in particular when tests are being setup and torn down repeatedly (when
      run using an ANT task).

      I for myself use a context for testing DAO's and integration tests / acceptance tests. The rest is tested using mocks (jMock). The context is loaded once and held static until an exception is thrown on tear down / start up or during a test. If the exceptions happens during the context loading, all tests are ended.
      Would you be able to share actual setup code for this. This is exactly the sort of thing I'm after...

      Comment

      Working...
      X