Announcement Announcement Module
Collapse
No announcement yet.
Problem with MVC testing and transaction manager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with MVC testing and transaction manager

    Dear colleagues,

    I am writing unit tests for Spring MVC application that originally works under Resin. Hibernate is configured to use JTA transaction manager (via transaction.manager_lookup_class property). Datasource is deployed to Resin as well and accessed via JNDI. I have successfully got the datasource available using SimpleNamingContextBuilder . But hibernate still requires "java:comp/TransactionManager" bound to instance of javax.transaction.TransactionManager.
    So I need either to bind some mock transaction manager to this name, or to intercept hibernate session factory work by saying not to look up it. What is the common approach for this ?
    Many thanks for your qualified help, as always !

  • #2
    Don't use JTA in your tests, use a local transactionmanager (HibernateTransactionManager). If you really want to use JTA use an Embedded Jetty instance to test your application.

    Also if you are writing tests which need your environment it probably is an integration test NOT a unit test. A unit test is testing a unit of work in Isolation. So no environment, not even Spring. Just your service and some stubs/mocks.
    Last edited by Marten Deinum; Sep 10th, 2008, 06:35 AM. Reason: .

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Don't use JTA in your tests, use a local transactionmanager (HibernateTransactionManager). If you really want to use JTA use an Embedded Jetty instance to test your application.
      I'd be happy to use HibernateTransactionManager, but hibernate configuration in this project is tied to Resin transaction manager :
      Code:
      <property name="transaction.manager_lookup_class">
      org.hibernate.transaction.ResinTransactionManagerLookup
      </property>
      Hibernate SessionFactoryImpl expects JTA TransactionManager in such case. I tried to create instance of HibernateTransactionManager and pass it under the same JNDI name ("java:comp/TransactionManager") , but it leads to ClassCastException .
      Or is it possible to substitute hibernate configuration file solely for test run ?

      Comment


      • #4
        Create a properties object named hibernateProperties, and include the default in the normal applicationContext. In your test configuration load an extra xml file overriding those properties.

        Code:
        <bean id="hibernateProperties" class="PropertiesFactoryBean">
          <property name="properties">
            <value>
              transaction.manager_lookup_class=org.hibernate.transaction.ResinTransactionManagerLookup
        
            <value>
          </property>
        </bean>
        In some applicationContext-test.xml

        Code:
        <bean id="hibernateProperties" class="PropertiesFactoryBean">
          <property name="properties">
            <value>
              //some hibernate properties
            <value>
          </property>
        </bean>
        
        <bean id="transactionManager" class="HibernateTransactionManager">
          .. other properties
        </bean>
        In your test case make sure the applicationContext-test.xml is loaded AFTER your other xml file. And you should be good to go.

        Comment


        • #5
          This part of Spring configuration files are 3rd party packed in jar , so I can not change them. Hibernate properties are not Spring bean in there. Thank you for the idea anyway.

          Comment

          Working...
          X