6 Replies Latest reply on Jan 2, 2008 5:32 AM by pmuir

    injection in tests ?

    berkay

      Hi.
      Want to test my action classes with hibernateSession instead of EntityManager and my actions like below..

      @In(create=true)
      private Session hibernateSession;
      
      @Out @In(required=false)
      Category category;
      
      public String save(){
       //bla bla
       hibernateSession.saveOrUpdate(category);
       hibernateSession.flush();
       return "success";
       }



      when i want to test this action with testNG, hibernate session doesnt initialize..

      @Test
       public void testAddCategory() throws Exception {
       Category cat = new Category();
       cat.setName("test");
       CategoryAction action = new CategoryAction();
       action.setCategory(cat);
       //
       assert "categoryTree".equals(action.save());//throws nullpointer because of the not initialized hibernate session
       }



      and according to me seam doesn't works while testing because Logger and In annotation doesnt works either..
      All the examples are about EntityManagerFactory & EntityManager..

      How can i find a solution for this test case with hibernate session ??

      thanx.

        • 1. Re: injection in tests ?
          pmuir

          Are you running an integration or unit test?

          • 2. Re: injection in tests ?
            berkay

            will test both of them..
            how can i initialize hibernateSession ?

            • 3. Re: injection in tests ?
              pmuir

              It depends on which sort of test you are doing... So, which one?

              • 4. Re: injection in tests ?
                berkay

                Hi Pete,
                It must be unit testing. Here is what I have tried with best of my knowledge. Here is my @BeforeClass method :

                @BeforeClass
                 public void init(){
                 try {
                 HibernateSessionFactory hsf=new HibernateSessionFactory();
                 hibernateSession=hsf.getSessionFactory().openSession();
                 } catch (HibernateException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 } catch (Exception e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
                
                 }
                


                I get

                FAILED CONFIGURATION: @BeforeMethod begin
                java.lang.IllegalStateException: Attempted to invoke a Seam component outside the an initialized application
                 at org.jboss.seam.contexts.Lifecycle.getApplication(Lifecycle.java:36)
                 at org.jboss.seam.contexts.Lifecycle.beginSession(Lifecycle.java:173)
                 at org.jboss.seam.contexts.ServletLifecycle.beginSession(ServletLifecycle.java:124)
                 at org.jboss.seam.mock.BaseSeamTest.begin(BaseSeamTest.java:918)
                 at org.jboss.seam.mock.SeamTest.begin(SeamTest.java:28)
                


                Happy new year

                • 5. Re: injection in tests ?
                  berkay

                  any solution ?

                  • 6. Re: injection in tests ?
                    pmuir

                    If you are running SeamTest, then it should be an integration test. Post the whole log from the failed test, including the container start.