0 Replies Latest reply on Mar 21, 2013 7:33 AM by jobame

    RequestScoped bean not destroyed in arquillian test

    jobame

      In an Arquillian test I inject a Stateless Session Bean. That SLSB is used only in test for calling the method under test several times within the same test (and it helps for transaction demarcation). The method under test is in a class annotated with @RequestScoped.

       

      Now I would expect the request scoped ClassUnderTest be generated newly and destroyed for each method call. However, that is not the case. Log statements in a PostConstruct and PreDestroy method show the Bean is once created in the first call of testMethodCaller and destroyed only after the second call of testMethodCaller is done but not in between.

       

      This is a problem since the state of the ClassUnderTest is not newly initialized.

       

      How come the bean is not constructed each time? How would the test need to be written so it works?

       

      The arquillian test

       

      public class ClassUnderTest_Test {
      
                // @Deployment...
      
                @Inject
                private TheSLSBIf injectedSLSB;
      
                @Test
                @UsingDataSet({ "testInputFile.xml" })
                public void useCaseTest() {
                       injectedSLSB.testMethodCaller();
                       injectedSLSB.testMethodCaller();
                }
      }
      

       

      SLSB

       

      @Stateless
      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
      public class TheSLSB implements TheSLSBIf {
      
                @Inject
                private ClassUnderTestIf testClassInstance;
      
                 @PersistenceContext
                private EntityManager em;
      
                public void testMethodCaller() {
                          testClassInstance.methodUnderTest();
                }
      }
      

       

      Class under Test

       

      @RequestScoped
      public class ClassUnderTest implements ClassUnderTestIf {
      
                @Inject
                private SomeServiceIf service;
      
                private String myVariable;
      
                public void methodUnderTest() {
                       // myVariable = ...
                       // Database actions via injected dao's
                }
      }