5 Replies Latest reply on Dec 28, 2007 2:10 PM by xshuang

    TestNG and Seam Documentation 12.1. Unit testing Seam compon

    ffischer

      Is it possible that the unit test example in the seam documentation, Paragraph 12.1 is out of date?

      1. I have to extend SeamTest to get it work at all
      2. If I try to set up the entityManager within an method annotated with @Configuration(beforeTestClass=true), I get the following exception. What do I miss?

      INFO 04-09 19:05:46,558 (Log4JLogger.java:info:94) -Hibernate EntityManager 3.2.0.CR1
      INFO 04-09 19:05:46,584 (Log4JLogger.java:info:94) -Hibernate Annotations 3.2.0.CR1
      INFO 04-09 19:05:46,603 (Log4JLogger.java:info:94) -Hibernate 3.2 cr2
      INFO 04-09 19:05:46,608 (Log4JLogger.java:info:94) -hibernate.properties not found
      INFO 04-09 19:05:46,615 (Log4JLogger.java:info:94) -Bytecode provider name : cglib
      INFO 04-09 19:05:46,622 (Log4JLogger.java:info:94) -using JDK 1.4 java.sql.Timestamp handling
      FATAL 04-09 19:05:47,047 (Log4JLogger.java:fatal:124) -net/ipcore/irp/model/test/integr/cm/testng.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,102 (Log4JLogger.java:fatal:124) -seam.propertiesO-:-Ofalse
      FATAL 04-09 19:05:47,104 (Log4JLogger.java:fatal:124) -components.propertiesO-:-Ofalse
      FATAL 04-09 19:05:47,104 (Log4JLogger.java:fatal:124) -WEB-INF/web.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,106 (Log4JLogger.java:fatal:124) -WEB-INF/faces-config.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,108 (Log4JLogger.java:fatal:124) -WEB-INF/components.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,110 (Log4JLogger.java:fatal:124) -META-INF/persistence.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,111 (Log4JLogger.java:fatal:124) -META-INF/jboss-app.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,112 (Log4JLogger.java:fatal:124) -META-INF/ejb-jar.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,113 (Log4JLogger.java:fatal:124) -META-INF/application.xmlO-:-Ofalse
      FATAL 04-09 19:05:47,114 (Log4JLogger.java:fatal:124) -META-INF/.#persistence.xml.1.1O-:-Ofalse
      INFO 04-09 19:05:47,266 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.Subject
      INFO 04-09 19:05:47,300 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.Subject on table subject
      INFO 04-09 19:05:47,414 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.LegalPerson
      INFO 04-09 19:05:47,417 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.LegalPerson on table legal_person
      INFO 04-09 19:05:47,465 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.RegistrationProvider
      INFO 04-09 19:05:47,469 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.RegistrationProvider on table reg_provider
      INFO 04-09 19:05:47,470 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.Person
      INFO 04-09 19:05:47,471 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.Person on table person
      INFO 04-09 19:05:47,477 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.faktura.Article
      INFO 04-09 19:05:47,480 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.faktura.Article on table article
      INFO 04-09 19:05:47,494 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.product.Configuration
      INFO 04-09 19:05:47,495 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.product.Configuration on table configuration
      INFO 04-09 19:05:47,499 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.Address
      INFO 04-09 19:05:47,503 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.Address on table address
      INFO 04-09 19:05:47,515 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.PostalAddress
      INFO 04-09 19:05:47,519 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.PostalAddress on table postal_address
      INFO 04-09 19:05:47,539 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.finance.BankingAccount
      INFO 04-09 19:05:47,542 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.finance.BankingAccount on table banking_account
      INFO 04-09 19:05:47,557 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.finance.FiscalData
      INFO 04-09 19:05:47,560 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.finance.FiscalData on table fiscal_data
      INFO 04-09 19:05:47,563 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.faktura.PurchaseOrderItem
      INFO 04-09 19:05:47,564 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.faktura.PurchaseOrderItem on table purchase_order_item
      INFO 04-09 19:05:47,570 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.User
      INFO 04-09 19:05:47,572 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.User on table user
      INFO 04-09 19:05:47,650 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.faktura.PurchaseOrder
      INFO 04-09 19:05:47,651 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.faktura.PurchaseOrder on table purchase_order
      INFO 04-09 19:05:47,654 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.product.Product
      INFO 04-09 19:05:47,655 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.product.Product on table product
      INFO 04-09 19:05:47,662 (Log4JLogger.java:info:94) -Binding entity from annotated class: net.ipcore.irp.model.cm.CommAddress
      INFO 04-09 19:05:47,663 (Log4JLogger.java:info:94) -Bind entity net.ipcore.irp.model.cm.CommAddress on table comm_address
      INFO 04-09 19:05:47,922 (Log4JLogger.java:info:94) -Mapping collection: net.ipcore.irp.model.cm.Subject.bankingAccounts -> banking_account
      INFO 04-09 19:05:47,926 (Log4JLogger.java:info:94) -Mapping collection: net.ipcore.irp.model.cm.Subject.orders -> purchase_order
      INFO 04-09 19:05:48,142 (Log4JLogger.java:info:94) -JNDI InitialContext properties:{}
      ERROR 04-09 19:05:48,160 (Log4JLogger.java:error:119) -Could not obtain initial context
      javax.naming.NamingException: Local server is not initialized
      at org.jnp.interfaces.LocalOnlyContextFactory.getInitialContext(LocalOnlyContextFactory.java:45)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
      at javax.naming.InitialContext.init(InitialContext.java:223)
      at javax.naming.InitialContext.(InitialContext.java:175)
      at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
      at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
      at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
      at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
      at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
      at net.ipcore.irp.model.test.unit.cm.CmUnitTest.init(CmUnitTest.java:39)
      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.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:552)
      at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:322)
      at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:156)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:97)
      at org.testng.TestRunner.privateRun(TestRunner.java:693)
      at org.testng.TestRunner.run(TestRunner.java:574)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:241)
      at org.testng.SuiteRunner.run(SuiteRunner.java:145)
      at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:901)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:863)
      at org.testng.TestNG.run(TestNG.java:613)
      at org.testng.eclipse.runner.RemoteTestNG.run(RemoteTestNG.java:85)
      at org.testng.eclipse.runner.RemoteTestNG.main(RemoteTestNG.java:127)
      FATAL 04-09 19:05:48,166 (Log4JLogger.java:fatal:129) -Could not find datasource: java:/DefaultDS
      javax.naming.NamingException: Local server is not initialized
      at org.jnp.interfaces.LocalOnlyContextFactory.getInitialContext(LocalOnlyContextFactory.java:45)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
      at javax.naming.InitialContext.init(InitialContext.java:223)
      at javax.naming.InitialContext.(InitialContext.java:175)
      at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
      at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
      at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
      at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
      at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
      at net.ipcore.irp.model.test.unit.cm.CmUnitTest.init(CmUnitTest.java:39)
      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.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:552)
      at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:322)
      at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:156)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:97)
      at org.testng.TestRunner.privateRun(TestRunner.java:693)
      at org.testng.TestRunner.run(TestRunner.java:574)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:241)
      at org.testng.SuiteRunner.run(SuiteRunner.java:145)
      at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:901)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:863)
      at org.testng.TestNG.run(TestNG.java:613)
      at org.testng.eclipse.runner.RemoteTestNG.run(RemoteTestNG.java:85)
      at org.testng.eclipse.runner.RemoteTestNG.main(RemoteTestNG.java:127)
      Creating /home/ff/projects/irp-seam/tmp/test-output/irp-seam/net.ipcore.irp.model.test.unit.cm.CmUnitTest.html
      FAILED: init
      javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not find datasource
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:217)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
      at net.ipcore.irp.model.test.unit.cm.CmUnitTest.init(CmUnitTest.java:39)
      Caused by: org.hibernate.HibernateException: Could not find datasource
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
      at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
      at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:61)
      at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
      at org.hibernate.ejb.Ejb3Configuration.createFactory(Ejb3Configuration.java:151)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:205)
      ... 21 more
      Caused by: javax.naming.NamingException: Local server is not initialized
      at org.jnp.interfaces.LocalOnlyContextFactory.getInitialContext(LocalOnlyContextFactory.java:45)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
      at javax.naming.InitialContext.init(InitialContext.java:223)
      at javax.naming.InitialContext.(InitialContext.java:175)
      at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
      ... 30 more
      ... Removed 17 stack frames
      FAILED: end
      java.lang.NullPointerException
      at org.jboss.seam.contexts.WebApplicationContext.get(WebApplicationContext.java:47)
      at org.jboss.seam.contexts.WebApplicationContext.get(WebApplicationContext.java:80)
      at org.jboss.seam.core.Init.instance(Init.java:50)
      at org.jboss.seam.Component.getInstanceFromFactory(Component.java:1283)
      at org.jboss.seam.Component.getInstance(Component.java:1260)
      at org.jboss.seam.Component.getInstance(Component.java:1253)
      at org.jboss.seam.Component.getInstance(Component.java:1235)
      at org.jboss.seam.core.Manager.instance(Manager.java:303)
      at org.jboss.seam.contexts.Lifecycle.endSession(Lifecycle.java:187)
      at org.jboss.seam.mock.SeamTest.end(SeamTest.java:297)
      ... Removed 19 stack frames
      SKIPPED: testUser
      SKIPPED: reopenUser
      SKIPPED: destroy
      SKIPPED: cleanup

        • 1. Re: TestNG and Seam Documentation 12.1. Unit testing Seam co

          You can't depend on the EJB3 container being initialized in a @BeforeClass/beforeTestClass method, because it does in fact run before the embedded container is started by SeamTest.init().

          I believe TestNG will run tests in the order they're specified in the testng.xml file, so you could define an empty test (inheriting from SeamTest) that gets the container initialization done and have it run before any of your other tests.

          Speaking of things out of date, Seam is using a pretty old version of TestNG. Any plans to update it to 5.1?

          • 2. Re: TestNG and Seam Documentation 12.1. Unit testing Seam co
            gemel

            I had this failure too and I could solve it I hope I can help you.

            Dou you have a datasource defined in your persistence.xml file like this?
            <jta-data-source>java:/SeamRcAdfDatasource</jta-data-source>

            if yes --> Try this
            1- Take the <jta-data-source> definition away from the persistence.xml file

            2- As the test runs outside JBoss, the datasource you defined in xxx-ds.xml is not bound.
            So you have to create the EntityManagerFactory like this:
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("hibernate.show_sql", "true");
            ...
            map.put("hibernate.hbm2ddl.auto", "create-drop");
            map.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
            map.put("hibernate.connection.username", "username");
            map.put("hibernate.connection.password", "pwd");
            map.put("hibernate.connection.url", "connectionUrl");
            ...

            EntityManagerFactory emf = Persistence.createEntityManagerFactory("unitname", map);

            • 3. Re: TestNG and Seam Documentation 12.1. Unit testing Seam co
              eagleeye

               

              "chuckadams" wrote:
              You can't depend on the EJB3 container being initialized in a @BeforeClass/beforeTestClass method, because it does in fact run before the embedded container is started by SeamTest.init().


              Yep got the same problem. But with your idea you will get the same problem. It's not workin, even the empty test class inheriting from SeamTest with a @BeforeClass/beforeTestClass method is executed before the EJB3 container being initialized. I'm couldn't solve this problem.

              Not a good work around is to do the init method for the DS without any @Before.... And call this method from every test class with
              protected void initDataSource(){
               emf = Persistence.createEntityManagerFactory("mediportal");
              }
              


              and call this method in every TestClass
              with
              @Configuration(beforeTestClass = true)
              public void initDS() {
               initDataSource();
              }
              


              That means the DS will be initialized before every TestClass. It's stupid, cuz if you would write a test suite there is no sense to init the DS in every class.


              Speaking of things out of date, Seam is using a pretty old version of TestNG. Any plans to update it to 5.1?

              This is also a really good question.

              • 4. Re: TestNG and Seam Documentation 12.1. Unit testing Seam co
                pmuir

                CVS uses Testng 5.6

                • 5. Re: TestNG and Seam Documentation 12.1. Unit testing Seam co
                  xshuang

                  Good afternoon gemel,

                  Could you post your complete code segment? I followed your example to manually create an EntityManagerFactory , but got the following error:
                  No Persistence provider for EntityManager named XXX

                  Thanks,
                  Sheng