Seam Framework中的非法状态异常

时间:2010-03-30 16:43:28

标签: java seam

我有这个简单的课程:

    public class LuceneUtil{

    private final EntityManager entityManager;

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

//other code
}

如果我“正常”使用这个类(我的意思是部署等)一切都很好。 但是,如果我尝试从我的测试类中使用它,当它尝试加载entityManager时,它会给出:

  

java.lang.IllegalStateException:没有活动的应用程序上下文

有没有人有任何想法:为什么?

堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:2)

如果没有正确的Seam应用程序上下文设置,则无法在测试用例中使用Seam组件。这意味着,您必须模拟部署Seam框架。通常,您可以通过使用Seam的测试框架并在运行测试套件时在类路径中使用嵌入式JBoss来实现这一点。如果按以下方式使用组件,则会获得有效的应用程序上下文:

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();
    }
}

ComponentTest内,Seam会在执行测试用例期间注意您有正在运行的应用程序上下文。有关详细信息,请参阅Testing Seam applications