3 Replies Latest reply on Jun 1, 2006 2:39 AM by newlukai

    Configuring SEAM test with junit

    jxerxes

      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 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
          gavin.king

          This isn't really a Seam question. You need Emmanuel to answer this one. Try the JBoss or Hibernate EJB3 forums.

          • 2. Re: Configuring SEAM test with junit
            jxerxes

            Thanks for the quick answer! I'll try that...

            • 3. Re: Configuring SEAM test with junit

              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?

              I know, it might not be the right forum, but perhaps anybody has an idea?!?