4 Replies Latest reply on Feb 13, 2015 9:51 PM by tungtd

    Why CDI works with the managed container?

    tungtd

      After watched this video https://www.youtube.com/watch?v=YKZU9XNRRNY I thought we couldn't consume the resources such as EJB, CDI while using a managed container. Whereas I saw a test with a managed container can inject the CDI bean  arquillian-examples/GreeterTest.java at master · arquillian/arquillian-examples · GitHub.

       

      package org.arquillian.example;
      
      import javax.inject.Inject;
      import org.jboss.arquillian.container.test.api.Deployment;
      import org.jboss.arquillian.junit.Arquillian;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.asset.EmptyAsset;
      import org.jboss.shrinkwrap.api.spec.JavaArchive;
      import org.junit.Test;
      import org.junit.Assert;
      import org.junit.runner.RunWith;
      
      /**
       * @author <a href="http://community.jboss.org/people/dan.j.allen">Dan Allen</a>
       */
      @RunWith(Arquillian.class)
      public class GreeterTest {
      
          @Deployment
          public static JavaArchive createDeployment() {
              JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                  .addClasses(Greeter.class, PhraseBuilder.class)
                  .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
              // System.out.println(jar.toString(true));
              return jar;
          }
      
          @Inject
          Greeter greeter;
      
          @Test
          public void should_create_greeting() {
              Assert.assertEquals("Hello, Earthling!",
                  greeter.createGreeting("Earthling"));
              greeter.greet(System.out, "Earthling");
              //Assert.fail();
          }
      }
      

       

      Somebody help me understand this, please!

        • 1. Re: Why CDI works with the managed container?
          aslak

          Not sure I understand the question. Why wouldn't it work?

           

          Managed/Remote/Embedded containers are the 'same' in that respect. We still deploy to the container and run the test in side the container. All of them have access to CDI.

           

          You might be mixing this with RunMode, where if you RunAsClient you won't have access to CDI because it exists in the Container only.

          • 2. Re: Why CDI works with the managed container?
            tungtd

            Because according to the guy in the video said that:"Managed container run on different JVM with the test.". I can't picture how we consumed the Java EE resource outside the container. Is that true if I said the test method without @RunAsClient is run outside the container?

            • 3. Re: Why CDI works with the managed container?
              aslak

              "The guy in the video" is me..

               

              And correct, ManagedContainers run in a different JVM then from where the test is launched, but.. the actual execution of the Test it self is 'forwarded' to this new JVM and are still executed incontainer with full access to the services provided by the container.

               

              Technically when JUnit is launched from e.g. IDE the @Test method is overridden, invokes a Sevlet that is deployed in the container which again 'restarts the Test and executes the @Test method in side the new JVM and container. Then return the result back to the IDE as if it were executed in the same JVM.

              1 of 1 people found this helpful
              • 4. Re: Why CDI works with the managed container?
                tungtd

                I have to say "Wow". It's too complicated. Thank you Aslak