1 Reply Latest reply on May 21, 2014 9:46 AM by Juan Casta

    User Arquillian to test restful service with Alternative CDI

    Juan Casta Newbie

      Hi all.

       

      I need to use arquillian to test one EJB @Stateless configured as restful service that has inside him one cdi inyection of a second EJB. This second EJB goes to osb and returns a list of strings. For my test, I don´t want to go to OSB because I want to isolate my test environment with other environments.

       

      So, to achieve this I have managed to make an alternative of CDI for this second EJB in my test file. The CDI @Alternative returns always the same list string. In this way, I can test my EJB without depending the ways the EJB goes.

       

      This works cool when I call the EJB with Arquillian and CDI inyection (without using @RunAsClient)

       

      Sample code of my alternative:

      @Produces
        @AlternativeFactory
        public XXXEJB produceIntervinientesMock() throws Exception {
                 
          IntervinientesEJB intervinientes = mock(IntervinientesEJB.class);
      

       

       

      Sample code of my @Deployment:

      @Deployment
          public static Archive<?> createTestArchive() {
        WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
              .addClass(Logger.class)
              .addClass(MiscResourceProducer.class)
              .addClass(IntervinientesRS.class)
                      .addClass(AbstractBaseRS.class)
                      .addClass(JaxRsActivation.class)
              .addClass(IntervinientesEJB.class)
                      .addAsWebInfResource("beans.xml");
              System.out.println(war.toString(true));
              return war;
      
      
          }
      

       

      The beans.xml has the alternative pre-configured (@AlternativeFactory).

       

      Sample code of the test in which works the alternative:

      @Test
        public void getListIntervinientesSSOMockTest() throws BusinessException, Exception {
      
      //CODE REMOVED
        StringList list = intervinientesRS.getListIntervinientes(0);
        Assert.assertNotNull(list);
        
      
        }
      

       

      The alternative works fine. I can access the EJB inyected via CDI and obtain the alternative.

       

      The problem is when I want to test this, but in client mode. I want to access the EJB in client mode, and when the function goes to the EJB, obtain the @Alternative, instead of the proper EJB.

       

      Questions are,

       

      1. Is it possible? Alternative works in a test that is not @RunAsClient. I am calling outside the war.

      2. If it is not possible, is well focused my approach?

       

       

      Regards in advantaje.

       

       

       

      The problem

        • 1. Re: User Arquillian to test restful service with Alternative CDI
          Juan Casta Newbie

          Hi all.

           

          I solved it.

           

          The alternative was not executed because it was located inside my test class, inside the war deployed with arquillian.

           

          @Produces  
            @AlternativeFactory  
            public XXXEJB produceIntervinientesMock() throws Exception {  
                       
              IntervinientesEJB intervinientes = mock(IntervinientesEJB.class);  
          
          

           

          So, we moved it inside a class inside srr/main/ and imported this class with arquillian. Once done it, the alternative worked and we could test our rest service from @RunAsClient with CDI Alternative and Arquillian.

           

          Thanks.