2 Replies Latest reply on Nov 10, 2008 7:19 AM by sujeet

    Jnit with EntityManager

    sujeet
      Hi Guys,

      I am trying to test Entity Manager driven class from Junit with the following code.
      Trying to add some entry to the database.
      The persistence unit to my persistence.xml is "provisioning".

      public class UserRoleTest extends TestCase {
      private static EntityManagerFactory emf;
          protected static EntityManager em;
           
           
          public EntityManagerFactory getEntityManagerFactory()
          {
              return emf;
          }
           
          @BeforeClass
          public static void setUpBeforeClass() throws Exception
          {
           emf = Persistence.createEntityManagerFactory("provisioning");
           em =emf.createEntityManager();
          }

          @AfterClass
          public static void tearDownAfterClass() throws Exception
          {
              em.close();
           emf.close();
          }
           
          @Test
          public void testUserRole() throws Exception {
           UserRole userRole = new UserRole();
           userRole.setIdUserRole(3);
           userRole.setRole("minoadmin1");
           userRole.setRoleDescription("admin123");
                
           
          }
      }


      I am using eclipse IDE. Without any error test gets compiled but there is neither any output nor any entry to database.

      Can any one Please explain what is happening at the background.

      Thanks,
      Sujeet
        • 1. Re: Jnit with EntityManager
          obfuscator

          Hi! Check the documentation on unit testing. You need to write tests as ComponentTests in order to be able to do injections.

          • 2. Re: Jnit with EntityManager
            sujeet
            Hi Alexander,

            Thanks for the response.
            I tried using Component Test also.Code snippets given below:


            public class UserRoleTest1 extends SeamTest{
                 
            private EntityManagerFactory emf;
                 
            public EntityManagerFactory getEntityManagerFactory()
                {
                    return emf;
                }
                 
            @BeforeClass
            public void init() throws Exception
                {
                      super.init();
                      emf = Persistence.createEntityManagerFactory("provisioning");
                }

            @AfterClass
            public void destroy()
                {
                    emf.close();
                }
                 
                 
                 
            @Test
            public void testUserRole() throws Exception {

                      new ComponentTest(){

                           @Override
                           protected void testComponents() throws Exception {

                                UserRole userRole = new UserRole();
                                userRole.setIdUserRole(3);
                                userRole.setRole("minoadmin");
                                userRole.setRoleDescription("admin");

                           }
                      }.run();
            }
            }

                   

            But again the same result , test got compiled but then there was no output nor any entry to the database.


            Thanks,
            Sujeet