5 Replies Latest reply on Aug 9, 2013 12:19 AM by oehmsmith

    @RunAsClient - subsequent methods no @RequestScoped

    oehmsmith Newbie

      I have a problem.  I need to setup and tear down data and use @RunAsClient to test the RESTful services.  So I use JUnit's @FixMethodOrder(MethodSorters.NAME_ASCENDING) so I can have ordered test methods. I have a before method that does the setup, and end method that cleans the tables and a middle method that runs the test.  So I have three tests in this set.  I can't seem to have any more sets.  Infact the service I have @Injected no longer seems to be usable in the end method.  A ___ exception is thrown.

       

           @Test

          public void test01JSONSimple_00_DATALOAD() throws RollbackException, SecurityException, IllegalStateException, NotSupportedException, SystemException,

                  HeuristicMixedException, HeuristicRollbackException, javax.transaction.RollbackException {

              // Need to setup the database - can't do this in a @RunAsClient as need UserTransaction utx, only available on the server

              AllAlertTemplate data = (AllAlertTemplate) mergeToDB(historicalAlertServiceSUT.getService(), new AllAlertData02());

              // Also want to preventDBCleanup as the next @Test uses them

              preventDBCleanup = true;

              assertTrue(true);

          }

       

          @Test

          @RunAsClient

          public void test01JSONSimple_01(@ArquillianResource URL deploymentUrl) {

              String target = deploymentUrl.toString() + RESOURCE_PREFIX + "alertHist/json";

              Client client = ClientBuilder.newBuilder().register(JsonProcessingFeature.class).property(JsonGenerator.PRETTY_PRINTING, true).build();

              WebTarget path = client.target(target);

              JsonObject json = path.request().get(JsonObject.class);

              assertNotNull(json);

              System.out.println("Json:" + json);

              assertEquals(2, json.getJsonArray("alerts").size());

              preventDBCleanup = true;

          }

       

          @Test

          public void test01JSONSimple_02_DATACLEANUP() {

              // allow database cleanup to happen for the @RunAsClient testSomething00() where UserTransaction utx is undefined

              System.out.println("historicalAlertServiceSUT: " + historicalAlertServiceSUT);

              AbstractPersistService<?> service = historicalAlertServiceSUT.getService();

              System.out.println("service:" + service);

              assertTrue(true);

          }

       

      I see the data loading from test01JSONSimple_00_DATALOAD()  and the actual test retrieves data correctly.  But I never see the results from System.out.println("historicalAlertServiceSUT: " + historicalAlertServiceSUT); in test01JSONSimple_02_DATACLEANUP().  I see this exception:

       

      org.jboss.arquillian.test.spi.ArquillianProxyException: org.glassfish.hk2.api.MultiException : A MultiException has 1 exceptions.  They are:

      1. java.lang.IllegalStateException: Not inside a request scope.

      [Proxied because : Original exception not serializable, NotSerializableException

          at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2210)

          at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)

          at org.jvnet.hk2.internal.IterableProviderImpl.get(IterableProviderImpl.java:87)

          at org.glassfish.jersey.internal.inject.ReferencingFactory.provide(ReferencingFactory.java:101)

          at org.jvnet.hk2.internal.FactoryCreator.create(FactoryCreator.java:96)

          at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)

          at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:69)

          at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2204)

          at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:93)

          at org.glassfish.jersey.internal.inject.ContextInjectionResolver.resolve(ContextInjectionResolver.java:104)

          at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:771)

          at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:790)

          at org.glassfish.jersey.gf.cdi.CdiComponentProvider$1.inject(CdiComponentProvider.java:316)

          at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:158)

          at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:103)

          at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:93)

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

          at au.csiro.esa.rest.HistoricalAlertService$Proxy$_$$_WeldClientProxy.toString(Unknown Source)

          at java.lang.String.valueOf(String.java:2854)

          at java.lang.StringBuilder.append(StringBuilder.java:128)

          at au.csiro.esa.rest.HistoricalAlertService_REST_01_IT.test01JSONSimple_02_DATACLEANUP(HistoricalAlertService_REST_01_IT.java:125)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

          at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

          at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

          at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

          at org.jboss.arquillian.junit.Arquillian$6$1.invoke(Arquillian.java:270)

          at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

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

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

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

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

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

          at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)

          at org.jboss.arquillian.container.test.impl.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38)

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

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

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

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

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

          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

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

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

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

          at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

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

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

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

          at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

          at java.lang.reflect.Method.invoke(Method.java:601)

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

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

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

          at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:111)

          at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:263)

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

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

          at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

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

          at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)

          at org.junit.rules.RunRules.evaluate(RunRules.java:20)

          at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

          at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

          at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

          at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

          at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

          at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

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

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

          at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)

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

          at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

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

          at org.junit.runner.JUnitCore.run(JUnitCore.java:160)

          at org.junit.runner.JUnitCore.run(JUnitCore.java:138)

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

          at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:160)

          at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:126)

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

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

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

          at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)

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

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

          at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)

          at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)

          at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)

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

          at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)

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

          at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)

          at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)

          at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)

          at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)

          at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)

          at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)

          at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)

          at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)

          at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)

          at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)

          at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)

          at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)

          at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)

          at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)

          at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)

          at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)

          at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)

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

      Caused by: java.lang.IllegalStateException: Not inside a request scope.

          at com.google.common.base.Preconditions.checkState(Preconditions.java:149)

          at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:225)

          at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:153)

          at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2204)

          ... 124 more

       

       

      The HistoricalAlertService is:

       

      @Path("/alertHist")

      @RequestScoped

      public class HistoricalAlertService implements AbstractRestServices {

          @Inject

          private Logger logger;

       

          @Inject

          private AlertService alertService;

       

          @Context

          private HttpServletResponse response;

          @Context

          private HttpServletRequest request;

       

          @Override

          public AbstractPersistService<?> getService() {

              return alertService;

          }

       

          @GET

          @Path("xml")

          @Produces(MediaType.APPLICATION_XML)

          public AlertsDTO getAlertHistXML(@QueryParam("queryType") String queryTypeParam, @QueryParam("from") String fromParam, @QueryParam("to") String toParam,

                  @QueryParam("listTweetIds") String listTweetIdsParam) {

       

              // List<AlertX> x = getDataList();// getAlertHistX(null, null, null, null);//queryTypeParam, fromParam, toParam, listTweetIdsParam);

              // List<AlertX> x = getAlertHistX(queryTypeParam, fromParam, toParam, listTweetIdsParam);

              beginResponse();

              AlertsDTO x = getAlertHist(queryTypeParam, fromParam, toParam, listTweetIdsParam);

              System.out.println("-> getAlertHistX - lax:" + x);

              return x;

          }

       

          ...

       

      My solution has been to only have one test set per test class.  Though I hoping that this is a known problem and a solution is at hand.

       

      Any such luck?

       

      Thanks,

       

      Brooke

        • 1. Re: @RunAsClient - subsequent methods no @RequestScoped
          Aslak Knutsen Master

          Sorry, no such luck.

           

          Would you be able to attach a minimal working(or not working in this case) example project and I'll have a look at it.

          • 2. Re: @RunAsClient - subsequent methods no @RequestScoped
            oehmsmith Newbie

            Bummer, not a known problem then.  Or maybe that's good as there may just be something I haven't configured or written properly.

             

            I haven't completed the minimal test case in the time I had allocated and expect to get this done tomorrow.

            • 3. Re: @RunAsClient - subsequent methods no @RequestScoped
              oehmsmith Newbie

              I worked out the problem.

               

              Its the following as class variables:

               

              @Context
              private HttpServletResponse response;
              @Context
              private HttpServletRequest request;
              

               

              If they are moved to being method variables then it works:

               

                  @GET
                  @Path("json")
                  @Produces({ MediaType.APPLICATION_JSON })
                  public AlertsDTO getAlertHistJSON(
                          @QueryParam("queryType") String queryTypeParam,
                          @QueryParam("from") String fromParam,
                          @QueryParam("to") String toParam,
                          @QueryParam("listTweetIds") String listTweetIdsParam, @Context HttpServletResponse response, @Context HttpServletRequest request) {
              

               

              However I thought this to be quite ugly and crowded and so preferred the class variables option.

               

              I tried @Produces'ing them but the WELD DEPENDENCIES 1408 error occurs (which one would expect I 'spose).

               

              Can this be fixed - I hate the tail wagging the dog (tests driving hte production code)?

               

              What's funny is that the URIInfo doesn't cause the same problem:

               

              @Context
                  private UriInfo uriInfo;
              

               

              Cheers,

               

              Brooke

               

              Message was edited by: Brooke Smith

              • 4. Re: @RunAsClient - subsequent methods no @RequestScoped
                oehmsmith Newbie

                Attaching minimal test project.  Its from a 'mvn install' so the unit test is passing - comment out the two @Contexts to see it fail.

                • 5. Re: @RunAsClient - subsequent methods no @RequestScoped
                  oehmsmith Newbie

                  Any ideas about if this is fixable?

                   

                  I have the above work-around or the below so they are still class variables. I'm just concerned there may be some bigger problem here.

                   

                  @Path("/blah")
                  class BlahRest {
                       private HttpServletResponse response;
                       private HttpServletRequest request;
                  
                       ....
                  
                       @GET
                       @Produces(MediaType.APPLICATION_XML)
                       public BlahDTO getBlahXML(@QueryParam String param1, @QueryParam String param2, ..., @Context HttpServletResponse response, @Context HttpServletRequest request) {
                            this.request = request;
                            this.response = response;
                            prepareResponse();
                  
                            ....
                       }
                  
                       private prepareResponse() {
                            response.set....();
                            ...