0 Replies Latest reply on Oct 31, 2010 9:05 PM by Ove Ranheim

    ManagedScope vs Stateful + Named

    Ove Ranheim Novice

      Based on Arquillian's Showcase JSFUnitTestCase, I'm investigating the difference between ManagedScope vs Stateful and Named. I noticed that ManagedScope is not the same as a Stateful and Named bean. According to the Weld documentation it states it should not be necessary to use ManagedScope, since the Stateful architecture of Weld/Seam will manage that.


      package com.acme.jsf;
      
      import javax.enterprise.context.RequestScoped;
      import javax.faces.bean.ManagedBean;
      
      
      @ManagedBean(name="requestBean")
      @RequestScoped
      public class RequestScopeBean extends ScopeAwareBean 
      {
          
      }
      




      When I change this to:



      @Stateful
      @RequestScoped
      @Named(value="requestBean")
      
      




      The test case fails with:



      -------------------------------------------------------------------------------
      Test set: com.acme.jsf.JSFUnitTestCase
      -------------------------------------------------------------------------------
      Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 15.983 sec <<< FAILURE!
      shouldExecutePage(com.acme.jsf.JSFUnitTestCase)  Time elapsed: 0.53 sec  <<< ERROR!
      java.lang.NullPointerException
           at org.jboss.jsfunit.jsfsession.JSFServerSession.getManagedBeanValue(JSFServerSession.java:160)
           at com.acme.jsf.JSFUnitTestCase.shouldExecutePage(JSFUnitTestCase.java:65)
           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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
           at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
           at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
           at org.jboss.arquillian.junit.Arquillian$6$1.invoke(Arquillian.java:251)
           at org.jboss.arquillian.impl.handler.TestEventExecuter.callback(TestEventExecuter.java:44)
           at org.jboss.arquillian.impl.handler.TestEventExecuter.callback(TestEventExecuter.java:34)
           at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:63)
           at org.jboss.arquillian.impl.context.AbstractEventContext.fire(AbstractEventContext.java:115)
           at org.jboss.arquillian.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:160)
           at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:244)
           at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:207)
           at org.jboss.arquillian.junit.Arquillian$5$1.evaluate(Arquillian.java:225)
           at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
           at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:221)
           at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
           at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
           at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
           at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
           at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
           at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
           at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
           at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:163)
           at org.jboss.arquillian.junit.Arquillian$3$1.evaluate(Arquillian.java:186)
           at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
           at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:182)
           at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
           at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:127)
           at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
           at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
           at org.jboss.arquillian.junit.JUnitTestRunner.execute(JUnitTestRunner.java:69)
           at org.jboss.arquillian.protocol.servlet_3.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.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:63)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
           at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:68)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
           at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
           at org.jboss.arquillian.framework.jsfunit.JSFUnitCleanupTestTreadFilter.doFilter(JSFUnitCleanupTestTreadFilter.java:52)
           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:93)
           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:653)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
           at java.lang.Thread.run(Thread.java:680)
      
      shouldExecutePage2(com.acme.jsf.JSFUnitTestCase)  Time elapsed: 0.092 sec  <<< ERROR!
      java.lang.NullPointerException
           at org.jboss.jsfunit.jsfsession.JSFServerSession.getManagedBeanValue(JSFServerSession.java:160)
           at com.acme.jsf.JSFUnitTestCase.shouldExecutePage2(JSFUnitTestCase.java:80)
           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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
           at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
           at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
           at org.jboss.arquillian.junit.Arquillian$6$1.invoke(Arquillian.java:251)
           at org.jboss.arquillian.impl.handler.TestEventExecuter.callback(TestEventExecuter.java:44)
           at org.jboss.arquillian.impl.handler.TestEventExecuter.callback(TestEventExecuter.java:34)
           at org.jboss.arquillian.impl.event.MapEventManager.fire(MapEventManager.java:63)
           at org.jboss.arquillian.impl.context.AbstractEventContext.fire(AbstractEventContext.java:115)
           at org.jboss.arquillian.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:160)
           at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:244)
           at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:207)
           at org.jboss.arquillian.junit.Arquillian$5$1.evaluate(Arquillian.java:225)
           at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
           at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:221)
           at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
           at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
           at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
           at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
           at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
           at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
           at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
           at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:163)
           at org.jboss.arquillian.junit.Arquillian$3$1.evaluate(Arquillian.java:186)
           at org.jboss.arquillian.junit.Arquillian$MultiStatementExecutor.execute(Arquillian.java:297)
           at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:182)
           at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
           at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:127)
           at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
           at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
           at org.jboss.arquillian.junit.JUnitTestRunner.execute(JUnitTestRunner.java:69)
           at org.jboss.arquillian.protocol.servlet_3.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.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:63)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
           at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:68)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
           at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
           at org.jboss.arquillian.framework.jsfunit.JSFUnitCleanupTestTreadFilter.doFilter(JSFUnitCleanupTestTreadFilter.java:52)
           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:93)
           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:653)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
           at java.lang.Thread.run(Thread.java:680)
      
      



      I also noticed that the el-name in @Named annotation, is not respected. You need to assign the el-name as param to the ManagedScope annotation.


      @Named(value=<el-name>)



      vs.



      @ManagedScope(name=<el-name>)




      Any logical reason for this behavior?