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) {
              if (entity.getId() <= 0) {
              else {
          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

          public class TestExmple {


              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
                          .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();




              public void testCanPersistUserObject() {
                  User u = new User();
                  List<User> users = userRepository.findUserByName("igor");
                  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:



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