2 Replies Latest reply on Apr 13, 2010 8:32 AM by Aslak Knutsen

    Problems with SessionContext while testing DAO

    Igor Gordiuchuk Newbie

      Hi guys,

        Thanks for realy good testing framework. So, i have some problems while running my test.

         All my DAO's uses SessionContext. For example :

       

         public void save(T entity){
              Integer tenantId = getTenantId();
              if (tenantId != null) {
                  entity.setTenantId(tenantId);
              }
              if (entity.getId() <= 0) {
                  entityManager.persist(entity);
              }
              else {
                  entityManager.merge(entity);
              }       
          }
         
          private Integer getTenantId(){
              return (Integer) Contexts.getSessionContext().get(ContextKey.TENANT_ID);

       

          }

      So the testing crushes on getTenantId() because of Contexts.getSessionContext().

      I have tried to create method in DAO  : Contexts.getSessionContext().set(......) and using it in my @Test method, but it crushes too...

        Who can i set up my SessionContext in test, or the is another way?

        • 1. Re: Problems with SessionContext while testing DAO
          Igor Gordiuchuk Newbie

          My Test Code

          @RunWith(Arquillian.class)
          public class TestExmple {

           

              @Deployment
              public static JavaArchive createTestArchive() throws ClassNotFoundException {

           

                 Class cls = org.jboss.seam.contexts.Contexts.class;
                 Package pkg = cls.getPackage();
                  Class cls2 = org.jboss.seam.log.Logging.class;
                 Package pkg2 = cls2.getPackage();
                 Class cls3 = org.jboss.seam.intercept.Interceptor.class;
                 Package pkg3 = cls3.getPackage();
                  Class cls4 = org.jboss.seam.ejb.SeamInterceptor.class;
                 Package pkg4 = cls4.getPackage();
                  Class cls5 = org.jboss.seam.Component.class;
                 Package pkg5 = cls5.getPackage();
                   Class cls6 = org.jboss.seam.annotations.intercept.InterceptorType.class;
                 Package pkg6 = cls6.getPackage();

           

                  return Archives.create("test.jar", JavaArchive.class).addClasses(User.class, UserDao.class, UserDaoImpl.class,BasicDao.class,BasicDaoImpl.class
                          ,Entity.class,ContextKey.class,org.jboss.seam.contexts.Contexts.class)
                          .addPackage(pkg).addPackage(pkg2)
                          .addPackage(pkg3).addPackage(pkg4)
                          .addPackage(pkg5)
                          .addPackage(pkg6)
                          .addManifestResource(
                          "persistence.xml",
                          ArchivePaths.create("persistence.xml"))
                          .addManifestResource("jboss-saasp-ds.xml", ArchivePaths.create("jboss-saasp-ds.xml"))
                          .addManifestResource("ejb-jar.xml", ArchivePaths.create("ejb-jar.xml"));

           

              }

           

             // @EJB
              private UserDaoImpl userRepository=new UserDaoImpl();

           

           

           

              @Test
              public void testCanPersistUserObject() {
                  User u = new User();
                  userRepository.saveUser(u);
                  u.setName("igor");
                  u.setPassword("fsdf");
                  u.setRole("user");
                  u.setTenantId(1);
            
                  List<User> users = userRepository.findUserByName("igor");
                  Assert.assertNotNull(users);
                  Assert.assertTrue(users.size() == 1);

           

              }
          }

          • 2. Re: Problems with SessionContext while testing DAO
            Aslak Knutsen Master

            Basically the problem is that Seam is never started.

             

            Currently we don't have any special Seam 2 support in Arquillian, http://community.jboss.org/message/532388

            And at the time being we don't really support returning a WebArchive as your @Deployment so you could add the right SeamListeners etc in your web.xml.

             

            You can try to manually bootstrap Seam like the SeamListener with some Mocks, something like:

            {code:java}

             

               @Test    public void myTest()    {       HttpSession session = new MockHttpSession(new MockServletContext());       ServletLifecycle.beginSession(session);             ..testCode..             ServletLifecycle.endSession(session);    } {code}