Unlawful exclusion of staff in the seam structure

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 when deploying, etc.), everything works fine. But, if I try to use it from my test classes, when it tries to load entityManager, it gives:

java.lang.IllegalStateException: active application context

Does anyone have an idea: 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) 
+4
source share
1 answer

You cannot use Seam components inside test cases without properly setting the context of the Seam application. This means that you need to model the deployment of the Seam framework. You can usually achieve this by using the Seam test framework and having a built-in JBoss in your class path while your test suite is running. You get the working context of the application 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(); } } 

Inside ComponentTest Seam ensures that you run the application context while running your test cases. For more information, see Testing seam applications .

+2
source

Source: https://habr.com/ru/post/1305572/


All Articles