7 Replies Latest reply on Mar 14, 2013 4:18 AM by Tom Tom

    @postconstruct method not running

    Samitha Jayathilaka Newbie

      In one of my tests i inject a certain facade class(Class A) which is annotated @Stateless @Localbean.

      In this facade there is a method (foo()) which needs a certain other class to instantiate a session.

      This other class (Class B) is annotated @Singleton @LocalBean. I also injected Class B in to this Arquillian test as well. The session is initiated in the @postconstruct method of the Class B. But when i test the foo() method i get an error that the session is not initiated.

      I test if the B class is injected, ie. if it's not null. It is not Null.

       

      FYI. Class B is injected in to the Class A as well.

      All this things are working perfectly in the use of the actual application. But when i use it this way in the Arquillian test i get the problems.

        • 1. Re: @postconstruct method not running
          Samitha Jayathilaka Newbie

          This is the stack trace

          java.lang.RuntimeException: Could not invoke PostConstruct on the newly created bean instance

              at org.jboss.ejb3.singleton.impl.container.SingletonEJBInstanceManagerImpl.create(SingletonEJBInstanceManagerImpl.java:137)

              at org.jboss.ejb3.singleton.impl.container.SingletonEJBInstanceManagerImpl.get(SingletonEJBInstanceManagerImpl.java:152)

              at org.jboss.ejb3.singleton.impl.container.SingletonContainer.invoke(SingletonContainer.java:201)

              at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.invoke(AOPBasedSingletonContainer.java:474)

              at org.jboss.ejb3.nointerface.impl.invocationhandler.NoInterfaceViewInvocationHandler.invokeEndpoint(NoInterfaceViewInvocationHandler.java:143)

              at org.jboss.ejb3.nointerface.impl.invocationhandler.NoInterfaceViewInvocationHandler.access$000(NoInterfaceViewInvocationHandler.java:54)

              at org.jboss.ejb3.nointerface.impl.invocationhandler.NoInterfaceViewInvocationHandler$1.invoke(NoInterfaceViewInvocationHandler.java:103)

              at org.jboss.ejb3.sis.reflect.InterceptorInvocationHandler$1.proceed(InterceptorInvocationHandler.java:84)

              at org.jboss.ejb3.sis.InterceptorAssembly$1.proceed(InterceptorAssembly.java:82)

              at org.jboss.ejb3.nointerface.impl.async.AsyncClientInterceptor.invoke(AsyncClientInterceptor.java:119)

              at org.jboss.ejb3.sis.InterceptorAssembly$1.proceed(InterceptorAssembly.java:74)

              at org.jboss.ejb3.nointerface.impl.invocationhandler.ObjectMethodsInterceptor.invoke(ObjectMethodsInterceptor.java:78)

              at org.jboss.ejb3.sis.InterceptorAssembly$1.proceed(InterceptorAssembly.java:74)

              at org.jboss.ejb3.sis.InterceptorAssembly.invoke(InterceptorAssembly.java:90)

              at org.jboss.ejb3.sis.reflect.InterceptorInvocationHandler.invoke(InterceptorInvocationHandler.java:110)

              at org.jboss.ejb3.nointerface.impl.invocationhandler.NoInterfaceViewInvocationHandler.invoke(NoInterfaceViewInvocationHandler.java:115)

              at org.jboss.ejb3.proxy.javassist.JavassistInvocationHandlerAdapter.invoke(JavassistInvocationHandlerAdapter.java:71)

              at xxxxxxxxxxxxxxxxxxxxxxxx_$$_javassist_40.getSession(DroolsKnowledgeBase_$$_javassist_40.java)

              at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:305)

              at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)

              at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)

              at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:299)

              at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:126)

              at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:62)

              at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:125)

              at xxxxxxxxxxxxxxxxxx.org$jboss$weld$bean-jboss$classloader:id="vfs:$$$F:$jboss-6$0$0$Final$server$default$deploy$test$war"-SessionBean-DroolsKnowledgeBase_$$_WeldProxy.getSession(org$jboss$weld$bean-jboss$classloader:id="vfs:$$$F:$jboss-6$0$0$Final$server$default$deploy$test$war"-SessionBean-DroolsKnowledgeBase_$$_WeldProxy.java)

              at se.xxxxxxxxxx.biz.business.TestRuleEngineFacade.setUp(TestRuleEngineFacade.java:112)

              at org.jboss.arquillian.junit.Arquillian$StatementLifecycleExecutor.invoke(Arquillian.java:340)

              at org.jboss.arquillian.impl.execution.BeforeLifecycleEventExecuter.execute(BeforeLifecycleEventExecuter.java:45)

              at org.jboss.arquillian.impl.execution.BeforeLifecycleEventExecuter.on(BeforeLifecycleEventExecuter.java:40)

              at org.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

              at org.jboss.arquillian.impl.core.EventContextImpl.invokeObservers(EventContextImpl.java:98)

              at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:80)

              at org.jboss.arquillian.impl.TestContextHandler.createClassContext(TestContextHandler.java:68)

              at org.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

              at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:87)

              at org.jboss.arquillian.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:54)

              at org.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

              at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:87)

              at org.jboss.arquillian.impl.TestContextHandler.createTestContext(TestContextHandler.java:82)

              at org.jboss.arquillian.impl.core.ObserverImpl.invoke(ObserverImpl.java:90)

              at org.jboss.arquillian.impl.core.EventContextImpl.proceed(EventContextImpl.java:87)

              at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:126)

              at org.jboss.arquillian.impl.core.ManagerImpl.fire(ManagerImpl.java:106)

              at org.jboss.arquillian.impl.EventTestRunnerAdaptor.before(EventTestRunnerAdaptor.java:85)

              at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:210)

              at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:303)

              at org.jboss.arquillian.junit.Arquillian.access$300(Arquillian.java:45)

              at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:228)

              at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:173)

              at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:303)

              at org.jboss.arquillian.junit.Arquillian.access$300(Arquillian.java:45)

              at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:187)

              at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:127)

              at org.jboss.arquillian.junit.JUnitTestRunner.execute(JUnitTestRunner.java:69)

              at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:84)

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)

              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)

              at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)

              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)

              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)

              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

              at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)

              at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)

              at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)

              at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)

              at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)

              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

              at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)

              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

              at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)

              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)

              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

              at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)

              at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)

              at java.lang.Thread.run(Thread.java:662)

          Caused by: java.lang.RuntimeException: java.lang.NullPointerException

              at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.invokeCallback(AOPBasedSingletonContainer.java:1063)

              at org.jboss.ejb3.EJBContainer.invokePostConstruct(EJBContainer.java:1396)

              at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.postConstruct(AOPBasedSingletonContainer.java:760)

              at org.jboss.ejb3.singleton.impl.container.SingletonEJBInstanceManagerImpl.create(SingletonEJBInstanceManagerImpl.java:133)

          Caused by: java.lang.NullPointerException

              at org.jboss.ejb3.EJBContainer.getApplicationException(EJBContainer.java:509)

              at org.jboss.ejb3.singleton.aop.impl.ConstructionInvocationContextAdapter.getApplicationException(ConstructionInvocationContextAdapter.java:62)

              at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:163)

              at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:251)

              at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:392)

              at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invoke(CMTTxInterceptor.java:211)

              at org.jboss.ejb3.tx2.aop.CMTTxInterceptorWrapper.invoke(CMTTxInterceptorWrapper.java:52)

              at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80)

              at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)

              at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80)

              at org.jboss.ejb3.core.context.CurrentInvocationContextInterceptor.invoke(CurrentInvocationContextInterceptor.java:47)

              at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80)

              at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:90)

              at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_23710924.invoke(InvocationContextInterceptor_z_setup_23710924.java)

              at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80)

              at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)

              at org.jboss.aop.joinpoint.ConstructionInvocation.invokeNext(ConstructionInvocation.java:80)

              at org.jboss.ejb3.singleton.aop.impl.AOPBasedSingletonContainer.invokeCallback(AOPBasedSingletonContainer.java:1059)

          • 2. Re: @postconstruct method not running
            Aslak Knutsen Master

            Could you provide a simple project/test showing the error?

            • 3. Re: @postconstruct method not running
              Samitha Jayathilaka Newbie

              @Singleton

              @LocalBean

              public class SomeClass {

               

                    private static KnowledgeBase kBase = null;

               

                  @PostConstruct

                  private void init(){

                      logger.info("Initiating Drools Engine");

                      KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

                      kbuilder.add(ResourceFactory.newClassPathResource(DRL_PATH), ResourceType.DRL);

               

                      kBase = KnowledgeBaseFactory.newKnowledgeBase();

                      kBase.addKnowledgePackages(kbuilder.getKnowledgePackages());

                      logger.info("Drools Engine initialized.");

                  }

                       @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

                  public StatelessKnowledgeSession getSession() throws DroolsEngineException {

                      if (kBase == null) {

                         throw new SomeException(".... Engine is not initialized.");

                      }

                      return kBase.newStatelessKnowledgeSession();

                  }

              ^^^This is the class with the @postConstruct method.

              The bold lettered exception is what i get.

              ==============================================================================================================

              @Stateless

              @LocalBean

              public class SomeFacade {

               

                  @Inject

                  private SomeClass kBase;

               

               

                  @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

                  public Foo validate(List<InputItem> inputItems) throws SomeException {

                      StatelessKnowledgeSession kSession = kBase.getSession();

                      .... more ...

               

              ^^^ This is the facade. The bold lettered place is the place with the exception.

              ============================================================================================================

                @Inject

                      SomeFacade facade;

               

                      @Inject

                      someClass kbase;

               

              @Test

                          public void testPassingValidation(){

               

               

                      try {

                         rvem= ruleEngineFacade.validate(mockInputItems);

                      } catch (SomeException ex) {

                          Logger.getLogger(TestRuleEngineFacade.class.getName()).log(Level.SEVERE, null, ex);

                      }

                      assertFalse(rvem.hasRuleValidationError());

                  }

              ^^^this is the relevant portions of the test. The bold lettered place is the place with the exception. please refer the stack trace.

              • 4. Re: @postconstruct method not running
                rhinmass Newbie

                I am actually having the same, or a similar, problem:

                 

                @Singleton

                public class SomeClass

                {

                    public SomeClass()

                    {

                       System.out.println("SomeClass Constructor " );    

                    }

                   

                   

                  @PostConstruct

                  public void postConstruct()

                  {

                    try {

                      System.out.println("SomeClass postConstruct " );    

                    } finally {

                     

                    }

                  }

                }

                 

                I see the message "SomeClass constructor" printed, but not "SomeClass postConstruct". Should this work?

                • 5. Re: @postconstruct method not running
                  rhinmass Newbie

                  In my case, the problem is due to lazy initialization.  I added @Startup (after @Singleton) and it works as I expected.

                   

                  For the original problem, have you tried making init() public ?

                  • 6. Re: @postconstruct method not running
                    rhinmass Newbie

                    Also, have you tried adding a no-arg constructor?

                    • 7. Re: @postconstruct method not running
                      Tom Tom Newbie

                      I have this same problem, what is the solution? How from arquillian test call postconstruct method?