0 Replies Latest reply on Feb 4, 2011 2:22 PM by Adam Victor Brandizzi

    Injecting entity manager in TestNG execution

    Adam Victor Brandizzi Newbie

      Hello, all!


      We are migrating an application from Spring to Seam running on Tomcat. Now, I'm trying to port the tests written before the migration. In fact, right now I am trying to run one simple test using classes from the application.


      Our persistence.xml uses a data source named sicorp-js...


      <persistence-unit name="sicorpPU" transaction-type="RESOURCE_LOCAL">
           <provider>org.hibernate.ejb.HibernatePersistence</provider>   
           <jta-data-source>sicorp-js</jta-data-source>
           <!-- ... -->
      </persistence-unit>



      ...so I added a new data source with the given name to the file hsqldb-ds.xml from bootstrap/deploy:


      <local-tx-datasource>
         <jndi-name>sicorp-ds</jndi-name>
         <connection-url>jdbc:hsqldb:${jboss.server.data.dir}${/}hypersonic${/}localDB</connection-url>
         <driver-class>org.hsqldb.jdbcDriver</driver-class>
         <user-name>sa</user-name>
         <password></password>
         <min-pool-size>0</min-pool-size>
         <max-pool-size>20</max-pool-size>
         <idle-timeout-minutes>0</idle-timeout-minutes>
         <track-statements/>
         <security-domain>HsqlDbRealm</security-domain>
         <prepared-statement-cache-size>32</prepared-statement-cache-size>
         <depends>jboss:service=Hypersonic,database=localDB</depends>
      </local-tx-datasource>



      However, if I run a very simple test...


      public class UsuarioDAOTest extends SeamTest {
         @Test
         public void f() throws Exception {
            new ComponentTest() {
               @Override
               protected void testComponents() throws Exception {
                  UsuarioDAO usuarioDAO = (UsuarioDAO) Component.getInstance(UsuarioDAOBean.class);
                  List<Usuario> usuarios = usuarioDAO.listarTodosUsuarios();
                  assertNotNull(usuarios);
               }
            }.run();
         }
      }




      I got this error:


      org.jboss.seam.RequiredException: @In attribute requires non-null value: usuarioDAO.em
        at org.jboss.seam.Component.getValueToInject(Component.java:2335)
        at org.jboss.seam.Component.injectAttributes(Component.java:1736)
        at org.jboss.seam.Component.inject(Component.java:1554)
        at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:61)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97)
        at org.jboss.seam.util.Work.workInTransaction(Work.java:47)
        at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
        at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
        at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
        at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
        at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
        at br.com.visent.sicorp.server.dao.impl.UsuarioDAOBean_$$_javassist_seam_1.listarTodosUsuarios(UsuarioDAOBean_$$_javassist_seam_1.java)
        at br.com.visent.sicorp.server.dao.test.UsuarioDAOTest$1.testComponents(UsuarioDAOTest.java:24)
        at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:162)
        at br.com.visent.sicorp.server.dao.test.UsuarioDAOTest.f(UsuarioDAOTest.java:27)
        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:597)
        [TESTNG METHODS OMITTED]
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)



      I suspect that the sicorp-js in <jta-data-source>sicorp-js</jta-data-source> in the persistence.xml is not the correct name to retrieve the datasource. In fact, I could verify through debug that the entity manager inside UsuarionDAOBean is null, which makes me wonder if it is being created at all. Maybe some prefix is necessary to retrieve the datasource defined with the name <jndi-name>sicorp-ds</jndi-name>


      So, given this configuration, how could I retrieve the datasource in the persistence.xml? Or do you think it is another problem?