8 Replies Latest reply on Jun 1, 2006 2:38 AM by Jens Weintraut

    Configuring SEAM test with junit

    Gregor Grambow Newbie

      Hi to everybody and thanks for the big job you have done!
      i know that i am asking a silly question...

      i need some help with my first SEAM app.
      (i already posted this in the seam forum and gavin told me i should post it here because Emmanuel would know the answer)
      i already have a working app with a sybase ASA9 database.
      i'm using seam beta 1 and jboss 4.0.3SP1 without ejb rc4.
      works fine. i want to wait till everything is final until i migrate.

      now i want to write a test with junit. my problem is that i cant get the configuration into the entitymanagerfactory. the code looks like this:

      EntityManagerFactory emf;
      ...
      emf = Persistence.createEntityManagerFactory("test");
      

      persistence.xml looks like:
      <entity-manager>
      <name>
      test
      </name>
      
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
      
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseAnywhereDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.connection.driver_class" value="com.sybase.jdbc2.jdbc.SybDriver"/>
       <property name="hibernate.connection.username" value="..."/>
       <property name="hibernate.connection.password" value="..."/>
       <property name="hibernate.connection.url" value="jdbc:sybase:Tds:localhost:same as in my app"/>
       <property name="hibernate.max_fetch_depth" value="3"/>
       </properties>
      </entity-manager>
      

      i have also tried
      <persistence>
       <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
      
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
      
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseAnywhereDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       <property name="hibernate.connection.driver_class" value="com.sybase.jdbc2.jdbc.SybDriver"/>
       <property name="hibernate.connection.username" value="..."/>
       <property name="hibernate.connection.password" value="..."/>
       <property name="hibernate.connection.url" value="jdbc:sybase:Tds:same as in my app"/>
       <property name="hibernate.max_fetch_depth" value="3"/>
       </properties>
       </persistence-unit>
      </persistence>
      

      or the programmatic way:
      HashMap map = new HashMap();
      
       map.put("hibernate.connection.driver_class", "com.sybase.jdbc2.jdbc.SybDriver");
       map.put("hibernate.connection.url", "jdbc:sybase:Tds:localhost:same as in my app");
       map.put("hibernate.connection.username", "...");
       map.put("hibernate.connection.password", "...");
      
       emf = Persistence.createEntityManagerFactory("test",map);
      

      it always goes as far as:
      21.03.2006 10:33:09 org.hibernate.cfg.Environment <clinit>
      INFO: Hibernate 3.1
      21.03.2006 10:33:09 org.hibernate.cfg.Environment <clinit>
      INFO: hibernate.properties not found
      21.03.2006 10:33:09 org.hibernate.cfg.Environment <clinit>
      INFO: using CGLIB reflection optimizer
      21.03.2006 10:33:09 org.hibernate.cfg.Environment <clinit>
      INFO: using JDK 1.4 java.sql.Timestamp handling
      

      and then i get a persistenceException: no provider for entity manager named test.

      please help me, i don't know what i have forgotten...




        • 1. Re: Configuring SEAM test with junit
          Emmanuel Bernard Master

          I can't remember when we switched to the nexw persistence.xml version.
          Wich one works when you deploy it in your app? (Not in junit)

          • 2. Re: Configuring SEAM test with junit
            Gregor Grambow Newbie

            Hello!
            it's the first (old) one with <entity-manager>.


            • 3. Re: Configuring SEAM test with junit
              Emmanuel Bernard Master

              So maybe try

              <name>test</name>

              rather than
              <name>
              test
              </name>


              • 4. Re: Configuring SEAM test with junit
                Emmanuel Bernard Master

                enabling logs to debug might help too, you should see what HEM is trying to do

                • 5. Re: Configuring SEAM test with junit
                  Gregor Grambow Newbie

                  I've tried

                  <name>test</name>
                  

                  as well, but it doesn't work.
                  Do I need any additional Libraries because it's not running in the container?
                  Or is it possible that the file is simply not found?
                  I've located it in the package tests.
                  The test itself runs in tests.somepackage.
                  (also tried to have it in the same directory)

                  i really don't know what I've missed...

                  • 6. Re: Configuring SEAM test with junit
                    Emmanuel Bernard Master

                    what do you see ion the debug logs?

                    • 7. Re: Configuring SEAM test with junit
                      Emmanuel Bernard Master

                      also try to get results from Enumeration xmls = Thread.currentThread()
                      .getContextClassLoader()
                      .getResources( "META-INF/persistence.xml" );

                      It should return the url of the perisstence.xml file

                      • 8. Re: Configuring SEAM test with junit
                        Jens Weintraut Apprentice

                        Hi there,

                        I've got the same problem. I want to test my Seam application with JUnit 4.1 and JBoss Eclipse IDE 1.6.

                        I've a little test class that needs an EntityManager:

                        public class LoginActionTest {
                         private static EntityManagerFactory emf;
                         private EntityManager em;
                         private EntityTransaction transaction;
                        
                         @BeforeClass
                         public static void initClass() {
                         emf = Persistence.createEntityManagerFactory("aresDatabase");
                         }
                        
                         @Before
                         public void init() {
                         em = emf.createEntityManager();
                         transaction = em.getTransaction();
                         transaction.begin();
                         }
                        
                         @After
                         public void destroy() {
                         transaction.commit();
                         em.close();
                         }
                        
                         @AfterClass
                         public static void destroyClass() {
                         emf.close();
                         }
                        
                         @Test
                         public void login() {
                         User jewe = new User();
                         jewe.setID("JEWE");
                        
                         LoginAction loginAction = new LoginAction();
                         loginAction.setAresDatabase(em);
                         loginAction.setUser(jewe);
                        
                         assert "main".equals(loginAction.login());
                        
                         }
                        
                         public static junit.framework.Test suite() {
                         return new JUnit4TestAdapter(LoginActionTest.class);
                         }
                        }


                        I've included the junit41.jar, jboss-commons.jar, dom4j.jar and ciglib.jar. Without them there are NoClassDefFoundErrors. After including those jars I ran the test and Eclipse resp. JUnit tells me

                        Console wrote:
                        01.06.2006 08:34:11 org.hibernate.cfg.Environment <clinit>
                        INFO: Hibernate 3.2 alpha2
                        01.06.2006 08:34:11 org.hibernate.cfg.Environment <clinit>
                        INFO: hibernate.properties not found
                        01.06.2006 08:34:11 org.hibernate.cfg.Environment <clinit>
                        INFO: using bytecode reflection optimizer
                        01.06.2006 08:34:11 org.hibernate.cfg.Environment buildBytecodeProvider
                        INFO: Bytecode provider name : cglib
                        01.06.2006 08:34:11 org.hibernate.cfg.Environment <clinit>
                        INFO: using JDK 1.4 java.sql.Timestamp handling


                        and
                        Stacktrace wrote:
                        javax.persistence.PersistenceException: No Persistence provider for EntityManager named aresDatabase
                        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:41)
                        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
                        at com.idsscheer.ares.test.LoginActionTest.initClass(LoginActionTest.java:26)
                        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                        at java.lang.reflect.Method.invoke(Method.java:585)
                        at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
                        at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
                        at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
                        at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
                        at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:32)
                        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
                        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
                        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


                        I guess it's because I'm not running those tests in a server environment but directly from Eclipse. So all the config files are not found.

                        Is there any HowTo that describes how I can write JUnit tests in Eclipse for Seam/Hibernate applications?