Announcement Announcement Module
No announcement yet.
Junit tests for Spring code.. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Junit tests for Spring code..

    I'm new to the Spring/Hibernate world and hence, would like to learn a bit about testing such code. BTW, searched on google, found some articles but nothing too precise, so am hoping someone here could point me to the right place or give some tips from experience.

    Basically, there is a huge amount of Spring/Hibernate code in the applications I am working on (work on the backend) but there aren't unit tests and I've embarked on a project to add these tests. The problem I am facing is that EJBs are involved (just MDBs) as well and hence, it is not easy enough to get hold of the object that I need to test. In other words, I have a dependency on the EJB container loading the beans, etc.

    But, to make a long story short, if you could point me to a place where I could learn something about unit testing spring/hibernate applications, that would be great. Ideally, I'd like to use mock objects for the Spring tests (so that there is no DB dependency) but need to test the DAOs (with the db up and running) for the hibernate tests. Thanks.

  • #2
    If you are going to be testing your Spring objects with unit testing and mock objects, you need no special support other than the mock object library.

    But for integration test with your DAOs, you should take a look at the reference manual for information on integration testing. There are several classes that Spring provides to help you easily load application context files that contain your hibernate objects. Some even provide rolling back after each test to make sure you don't fill your database with test data.

    The big problem I see for you is working around your MDBs. I have worked around them myself by making sure I don't put logic in my MDBs. I wrap MDBs around POJOs that implement MessageListener, but don't carry all the other baggage of MDBs. The MDBs are a thin class that invokes the POJO MessageListener. That way you can write a test without an EJB container. You can just create your MessageListener POJO and invoke onMessage yourself.

    But if you need to reply to messages into a real queue you should take a look at the MockRunner library. It helps you do JMS testing outside the container. I've used this quite a bit to help me recreate our message flow without the EJB container.