testing transaction management in Junit

We have our own transaction mangager class that uses the TransactionHelper. Our Transaction manager changes the current transaction timeout and seems when our nuxeo system is running but not during Junit testing. I looked through the nuxeo documementation for information on configuring nuxeo unit tests but can't seem to get the configuration right so that the unit tests run the same way.

Do I need to be in an Operation or OperationMethod to be able to use the TransactionHelper? The Unit tests that run our existing Operations that try to change the transaction timeout are not able to set the time out in a unit test either.

I'm new to nuxeo, so sorry if this is a basic question, and the knowledgeable nuxeo developers at my company did not know the answer.

My Junit class's annotations are:

@RunWith(FeaturesRunner.class)
@Features({RuntimeFeature.class, CoreFeature.class})
@RepositoryConfig(type = BackendType.H2, user = "Administrator", init = DefaultRepositoryInit.class)
public class TransactionManagerTest {

and the test looks like this:

    try {
        javax.transaction.TransactionManager mgr = TransactionHelper.lookupTransactionManager();
        Assert.assertNotNull(mgr);
    } catch (NamingException e) {
        e.printStackTrace();         }
    try {
        javax.transaction.UserTransaction user = TransactionHelper.lookupUserTransaction();
    } catch (NamingException e) {
        e.printStackTrace();          }
    boolean results = TransactionHelper.startTransaction();
    // Assert.assertTrue("started a Transaction using the Transaction Helper",results);

For the lookup tests i get the following:

    DatabaseHelper: Database used for VCS tests: org.nuxeo.ecm.core.storage.sql.DatabaseH2
    SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
    SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
    javax.naming.NamingException: TransactionManager not found in JNDI
     at org.nuxeo.runtime.transaction.TransactionHelper.lookupTransactionManager(TransactionHelper.java:129)
     at com.jeppesen.gs.utils.transactions.TransactionManagerTest.testTransactions
     snip...

and the TransactionHelper.startTransaction() returns false.

0 votes

1 answers

3968 views

ANSWER



You need an actual transaction manager to be running for your tests. You can have one automatically created and registered in JNDI by adding to your list the feature TransactionalFeature, and also add the following annotation to your test class if you want to manually manage transaction start/stop in the test methods:

@TransactionalConfig(autoStart = false)
0 votes



Thanks, that worked (and now I know where to look for Features!) and I can create transactions but I'm getting the following warnings:

2012-07-18 12:07:33,520 WARN main org.nuxeo.ecm.core.api.TransactionalCoreSessionWrapper - Session invoked in a container without a transaction active java.lang.Throwable at org.nuxeo.ecm.core.api.TransactionalCoreSessionWrapper.checkTxActiveRequired(TransactionalCoreSessionWrapper.java:100) at org.nuxeo.ecm.core.api.TransactionalCoreSessionWrapper.invoke(TransactionalCoreSessionWrapper.java:125) at $Proxy21.createDocumentModel(Unknown Source) at org.nuxeo.ecm.core.test.DefaultRepositoryInit.populate(DefaultRepositoryInit.java:26) at org.nuxeo.ecm.core.test.CoreFeature.initializeSession(CoreFeature.java:156) at org.nuxeo.ecm.core.test.CoreFeature.beforeRun(CoreFeature.java:86) at org.nuxeo.runtime.test.runner.FeaturesRunner.beforeRun(FeaturesRunner.java:165) at org.nuxeo.runtime.test.runner.FeaturesRunner.run(FeaturesRunner.java:261) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs

07/18/2012

You can ignore this error for now, it's legitimate to call createDocumentModel without a transaction. We'll try to improve this message for the next release.
07/19/2012