Cristian Boariu
Cristian Boariu

Reputation: 9621

Illegal state exception in Seam Framework

I have this simple class:

    public class LuceneUtil{

    private final EntityManager entityManager;

public LuceneUtil() {
        entityManager = (EntityManager) Component.getInstance("entityManager");
    }

//other code
}

If i use this class "normally" (i mean with deploy and etc) all works well. But, if i try to use it from my test classes, when it tries to load the entityManager, it gives:

java.lang.IllegalStateException: No application context active

Does anyone has any ideea: why?

Stacktrace:

java.lang.IllegalStateException: No application context active
    at org.jboss.seam.Component.forName(Component.java:1945)
    at org.jboss.seam.Component.getInstance(Component.java:2005)
    at org.jboss.seam.Component.getInstance(Component.java:1983)
    at org.jboss.seam.Component.getInstance(Component.java:1977)
    at org.jboss.seam.Component.getInstance(Component.java:1972)
    at com.unifiedpost.my.docstore.query.LucenePropertyUtil.<init>(LucenePropertyUtil.java:27)
    at com.unifiedpost.my.docstore.query.PropertyQuery.<init>(PropertyQuery.java:16)
    at com.unifiedpost.my.docstore.query.EqualsPropertyQuery.<init>(EqualsPropertyQuery.java:16)
    at com.unifiedpost.docstore.it.rest.queries.QueryBuilder.prepareEquals(QueryBuilder.java:21)
    at com.unifiedpost.docstore.it.DocumentImporterTest.reindex(DocumentImporterTest.java:134)
    at com.unifiedpost.docstore.it.DocumentImporterTest.importDocumentAndIndexIt(DocumentImporterTest.java:107)
    at com.unifiedpost.docstore.it.rest.queries.InboxIndexQueryResourceBeanTest.testPropertyQuery(InboxIndexQueryResourceBeanTest.java:37)
    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:597)
    at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:517)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:669)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:956)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
    at org.testng.TestRunner.runWorkers(TestRunner.java:720)
    at org.testng.TestRunner.privateRun(TestRunner.java:590)
    at org.testng.TestRunner.run(TestRunner.java:484)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
    at org.testng.SuiteRunner.run(SuiteRunner.java:204)
    at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:864)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:830)
    at org.testng.TestNG.run(TestNG.java:748)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
java.lang.IllegalStateException: No application context active
    at org.jboss.seam.Component.forName(Component.java:1945)
    at org.jboss.seam.Component.getInstance(Component.java:2005)
    at org.jboss.seam.Component.getInstance(Component.java:1983)
    at org.jboss.seam.Component.getInstance(Component.java:1977)
    at org.jboss.seam.Component.getInstance(Component.java:1972)
    at com.unifiedpost.my.docstore.query.LucenePropertyUtil.<init>(LucenePropertyUtil.java:27)
    at com.unifiedpost.my.docstore.query.PropertyQuery.<init>(PropertyQuery.java:16)
    at com.unifiedpost.my.docstore.query.EqualsPropertyQuery.<init>(EqualsPropertyQuery.java:16)
    at com.unifiedpost.docstore.it.rest.queries.QueryBuilder.prepareEquals(QueryBuilder.java:21)
    at com.unifiedpost.docstore.it.DocumentImporterTest.reindex(DocumentImporterTest.java:134)
    at com.unifiedpost.docstore.it.DocumentImporterTest.importDocumentAndIndexIt(DocumentImporterTest.java:107)
    at com.unifiedpost.docstore.it.rest.queries.InboxIndexQueryResourceBeanTest.testPropertyQuery(InboxIndexQueryResourceBeanTest.java:37)
    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:597)
    at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:517)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:669)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:956)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
    at org.testng.TestRunner.runWorkers(TestRunner.java:720)
    at org.testng.TestRunner.privateRun(TestRunner.java:590)
    at org.testng.TestRunner.run(TestRunner.java:484)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
    at org.testng.SuiteRunner.run(SuiteRunner.java:204)
    at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:864)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:830)
    at org.testng.TestNG.run(TestNG.java:748)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)

Upvotes: 4

Views: 3412

Answers (1)

kraftan
kraftan

Reputation: 6312

You cannot use Seam components inside test cases without having a proper Seam application context setup. That means, you have to simulate that the Seam framework is deployed. Normally, you can achieve that by using the test framework of Seam and having an embedded JBoss in your classpath while running your test suite. You get a working application context, if you use your components as follows:

public class LuceneUtilTest extends SeamTest {

    @Test
    public void initComponentTest() throws Exception {
        new ComponentTest() {
            @Override
            protected void testComponents() throws Exception {
                LuceneUtil luceneUtilInstance = (LuceneUtil) Component.getInstance(LuceneUtil.class);
                // ...
            }
        }.run();
    }
}

Within a ComponentTest Seam takes care that you have a running application context during the execution of your test cases. For more information see Testing Seam applications.

Upvotes: 2

Related Questions