11 Replies Latest reply on Apr 4, 2006 12:05 PM by liudan2005

    How to get EntityManager in seamtest

    liudan2005

      Started trying seamtest again. How can i get EntityManager in SeamTest case.
      tried:
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydb");
      and tried:
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydb",configOverrides);

      None of them worked.

        • 1. Re: How to get EntityManager in seamtest
          gavin.king

          I don't understand. Usually you call your own components from SeamTest and EJB3 or Seam injects the EM.

          What is the context where you want to get one in your own code?

          • 2. Re: How to get EntityManager in seamtest
            liudan2005

            A lot of my entities has got listeners with business logic in. For example, in @PostPersist method I might caculate the balance for the entities. That's why I need em to test entities.

            • 3. Re: How to get EntityManager in seamtest
              liudan2005

              Has anyone tried? I now have to put all my test code into a ejb, which is not right.

              How do you usually test your entities with callback listeners?

              • 4. Re: How to get EntityManager in seamtest
                gavin.king

                Again, SeamTest starts Seam, which will start up the entitymanager you do NOT need to hack in your own entitymanager.

                You only need to do things like this for unit testing, not for integration testing.

                • 5. Re: How to get EntityManager in seamtest
                  liudan2005

                  OK. If I have code like this:

                  @Entity
                  public class Hotel implements Serializable
                  {
                  ...
                  @PrePersist
                  public void initialiseRank(){
                   int rank= caculate(a+b+c);
                   setRank(rank);
                  }
                  
                  @PreUpdate
                  public void generateHistoryRecord(){
                   save(getOriginalRecord())
                  }
                  
                  @PreUpdate
                  public void caculateRank(){
                   int rank= x + y + z;
                   setRank(rank);
                  }
                  
                  }
                  


                  my integration testing code would look like this:
                  Hotel hotel= new Hotel(name);
                  em.persist(hotel);
                  ...
                  Hotel savedHotel=em.find(Hotel.class, id);
                  assert checkHotelRank(savedHotel);
                  
                  savedHotel.getFeedbacks().add(feedback);
                  em.persist(savedHotel);
                  ...
                  assert checkHotelRank(savedHotel);
                  assert checkHotelHistory(savedHotel);
                  


                  As you can see, my testing code uses EntityManager. How would you write your test for this scenario without EntityManager?


                  • 6. Re: How to get EntityManager in seamtest
                    gavin.king

                    If that is all the code you have (ie. you have no Seam components that you are integration testing), this you should NOT be using SeamTest. SeamTest if for integration testing of Seam applications. This looks like a unit test.

                    • 7. Re: How to get EntityManager in seamtest
                      liudan2005

                      This scenario for me is more like an entity level integration test because a test case involved a set of entities e.g. Hotel + Feedback + History...

                      From what you said, SeamTest is not suitable for entity-only level tests. What would you recommand for our scenario? We basically need a way to test our entities.

                      Thanks for your help Gavin.

                      • 8. Re: How to get EntityManager in seamtest
                        gavin.king

                        Write a plain TestNG test. The way anyone tests Hibernate stuff today....

                        • 9. Re: How to get EntityManager in seamtest
                          liudan2005

                          It would be convienent if seam provides something like EntityTest. Had a look at http://blog.hibernate.org/cgi-bin/blosxom.cgi/2005/11/24#ejb3withtestng.

                          To test entites is not easy as what I thought. I have to get those xmls and re-config them, write code for starting up embeded container, use those ant script, jndi.... 2 hours gone, still can't get it working. One thing I noticed is it tests entiteis using DAO pattern , which I don't have.

                          What I need is simply having an EntityManager in a SeamTest and I guess it's a common requirement. Why do I need to get into a new world and config everything again?

                          It would make sense if Seam can cover this kind of tests.

                          • 10. Re: How to get EntityManager in seamtest
                            gavin.king

                            Huh???

                            No, not at all. You just start a persistence provider.

                            If all you are doing is testing *entities* you do NOT need an ejb3 container environment.

                            The only XML you need is persistence.xml.

                            • 11. Re: How to get EntityManager in seamtest
                              liudan2005

                              Thanks Gavin. Managed to get it working by doing this:

                               Map<String,String> configOverrides = new HashMap<String,String>();
                               configOverrides.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
                               configOverrides.put("hibernate.connection.url", "jdbc:hsqldb:.");
                              ...
                               EntityManagerFactory emf=Persistence.createEntityManagerFactory("bookingDatabase",configOverrides);
                               EntityManager em=emf.createEntityManager();
                              


                              Another thing(the last thing) is that I have to remove "jta-data-source" from persistance.xml in order to run this test. So I have to keep swap the xml file when I need to run this test. Is there a good way to avoid swaping xml?