Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
26. Jun 2008, 16:36 CET | Link

Hi

I'm trying to implement some tests on for my code. All my CRUD interface is based on SEAM and works fine. In addition, I've build an API to offer ability to manage data outside seam UI.

For my tests, I'm facing the following problem : my API is a singleton that stores current entityManager and persits transcientInstance via

getEntityManager().joinTransaction();
getEntityManager().persist(transientInstance);
getEntityManager().flush();

I've tried to build test on this and fail to make anything with transactions.

I finally tried the following dummy test :

public class MyApiTest extends SeamTest {
	@Test
	public void testLM0001() {
		try {
			UserTransaction uTx = super.getUserTransaction();
			uTx.begin();
			uTx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testLM0002() {
		try {
			Transaction.instance().begin();
			Transaction.instance().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

and both tests are failing with error No event context active

java.lang.IllegalStateException: No event context active
	at org.jboss.seam.ScopeType.getContext(ScopeType.java:114)
	at org.jboss.seam.Component.getInstance(Component.java:1851)
	at org.jboss.seam.Component.getInstance(Component.java:1829)
	at org.jboss.seam.Component.getInstance(Component.java:1824)
	at org.jboss.seam.transaction.Transaction.instance(Transaction.java:36)
	at org.jboss.seam.mock.BaseSeamTest.getUserTransaction(BaseSeamTest.java:1010)
	at MyApiTest.testLM0001(LogManagerTest.java:20)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:604)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:470)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:564)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:830)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.runWorkers(TestRunner.java:678)
	at org.testng.TestRunner.privateRun(TestRunner.java:624)
	at org.testng.TestRunner.run(TestRunner.java:495)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:300)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:295)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:275)
	at org.testng.SuiteRunner.run(SuiteRunner.java:190)
	at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:792)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:765)
	at org.testng.TestNG.run(TestNG.java:699)
	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:122)

How can I get transaction working with SeamTest ? Am I wrong when I'm planning to manually manage transactions with MyApi ?

Thanks for your support :)

3 Replies:
26. Jun 2008, 16:50 CET | Link

This is a tad tricky. I don't remember excactly why I did what I did, but I got it working through using


class X extends SeamTest {

        @BeforeMethod
        @Override
        public void begin() {
                startTx();
                super.begin();
        }

        @AfterMethod
        @Override
        public void end() {
                super.end();
                endTx();
        }

}

I couldn't get Transaction.instance() to work, so I used InitalContext to look it up:


return (UserTransaction) context.lookup("UserTransaction");

It is possible that you can lookup the tx at the point in time that you are currently doing, but if I remember correctly there was some sort of problem with that approach.

Good luck.

26. Jun 2008, 17:23 CET | Link

Simply great!!! :)

The solution for those who'd be looking for similar problems :


public class MyApiTest extends SeamTest {
        UserTransaction uTx = null;

     @BeforeMethod
     @Override
     public void begin() {
                 try {
                        uTx= (UserTransaction) super.getInitialContext().lookup("UserTransaction");
                        uTx.begin();
                } catch (Exception e) {

                        e.printStackTrace();
                }
                super.begin();
     }

     @AfterMethod
     @Override
     public void end() {
             super.end();
                try {
                                uTx.rollback(); 
                        } catch (Exception e) {
                                e.printStackTrace();
                        } 
     }

@Test
...

I will also have a look at Cory's way of running tests.

Thanks for both :)

26. Jun 2008, 17:03 CET | Link

This error is the result of the fact that there isn't a Seam context active.

You need to wrap your test code in this:

new ComponentTest() { protected void testComponents() throws Exception { // Test code here } }.run();