6 Replies Latest reply on Jun 11, 2007 4:25 AM by Christian Bauer

    Problem injecting (@In) entityManger in TestNG unit test

    Roger Goldman Newbie

      I'm trying to cause the EntityManager to be injected into a unit test class running via TestNG, but am having no luck. I'm hoping someone here can shed some light as to why.

      Here's the steps I took (using Seam 1.2.1GA, JBoss4.2.0GA, JBossIDE 2.0.0Beta2)


      1. "seam setup" to create a brand new project "SimpleTest"
      2. "seam new-project"
      3. "seam explode"
      4. Using JBossIDE, created New General Project for SimpleTest
      5. Created a class SimpleTest as follows:
      package simpletest;
      
      import javax.persistence.EntityManager;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.mock.SeamTest;
      import org.testng.annotations.Test;
      
      @Name("SimpleTest")
      public class SimpleTest extends SeamTest {
      
       @Test
       public void theTest() {
       entityManager.getTransaction().begin(); // I get a NullPointerException here!!!
       entityManager.getTransaction().commit();
       }
      
       @In
       private EntityManager entityManager;
      }
      

      6. Created a TestNG configuration file as follows:
      
      <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
      <suite name="Test">
       <test verbose="10" name="TheSimpleTest" annotations="JDK">
       <packages>
       <package name="simpletest" />
       </packages>
       </test>
      </suite>
      

      7. Ran TestNG with the following classpath elements:

      1. \SimpleTest\embedded-ejb\conf
      2. \SimpleTest\resources
      3. \SimpleTest\classes
      4. C:\jboss-seam-1.2.1.GA\lib\*.jar
      5. C:\jboss-seam-1.2.1GA\jboss-seam.jar



      Out of the box, I got the following error:
      ...
      22:24:56,381 INFO [Initialization] two components with same name, higher precedence wins: org.jboss.seam.persistence.persistenceProvider
      22:24:56,501 INFO [Initialization] two components with same name, higher precedence wins: org.jboss.seam.security.identity
      ...
      Failed to invoke @Configuration method org.jboss.seam.mock.SeamTest.init:Could not create Component: org.jboss.seam.security.identity
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.begin() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.end() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.begin() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.end() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.cleanup() for class [TestClass class simpletest.SimpleTest]
      
      *********** INVOKED METHODS
      
       org.jboss.seam.mock.SeamTest.init() 22481956
       simpletest.SimpleTest.theTest() 22481956
      
      ***********
      
      Creating C:\Projects\SimpleTest\test-output\Test\TheSimpleTest.html
      FAILED CONFIGURATION: @BeforeClass init
      java.lang.RuntimeException: Could not create Component: org.jboss.seam.security.identity
       at org.jboss.seam.init.Initialization.addComponent(Initialization.java:865)
       at org.jboss.seam.init.Initialization.installComponents(Initialization.java:796)
       at org.jboss.seam.init.Initialization.init(Initialization.java:503)
       at org.jboss.seam.mock.SeamTest.init(SeamTest.java:701)
      Caused by: java.lang.IllegalArgumentException: no such setter method: org.jboss.seam.security.Identity.securityRules
       at org.jboss.seam.util.Reflections.getSetterMethod(Reflections.java:219)
       at org.jboss.seam.Component.initInitializers(Component.java:401)
       at org.jboss.seam.Component.<init>(Component.java:263)
       at org.jboss.seam.Component.<init>(Component.java:203)
       at org.jboss.seam.init.Initialization.addComponent(Initialization.java:851)
       ... 25 more
      ... Removed 22 stack frames
      SKIPPED CONFIGURATION: @BeforeMethod begin
      SKIPPED CONFIGURATION: @AfterMethod end
      SKIPPED CONFIGURATION: @AfterClass cleanup
      SKIPPED: theTest
      


      So, I commented out the following lines in components.xml:
       <drools:rule-base name="securityRules">
       <drools:rule-files>
       <value>/security.drl</value>
       </drools:rule-files>
       </drools:rule-base>
      
       <security:identity authenticate-method="#{authenticator.authenticate}"
       security-rules="#{securityRules}"/>
      

      and got much further, but still got a NullPointerException trying to reference the entityManager that I was hoping to have injected by the @In annotation. As you can see the "entityManager" component was initialized from components.xml but @In didn't seem to inject it.
      ...
      22:27:46,646 INFO [Initialization] two components with same name, higher precedence wins: org.jboss.seam.security.identity
      22:27:46,736 INFO [Initialization] two components with same name, higher precedence wins: org.jboss.seam.persistence.persistenceProvider
      22:27:46,766 INFO [Component] Component: org.jboss.seam.core.init, scope: APPLICATION, type: JAVA_BEAN, class: org.jboss.seam.core.Init
      22:27:46,946 INFO [Initialization] Installing components...
      22:27:46,986 INFO [Component] Component: entityManager, scope: CONVERSATION, type: JAVA_BEAN, class: org.jboss.seam.core.ManagedPersistenceContext
      ...
      22:27:47,397 INFO [Ejb] starting the embedded EJB container
      22:27:48,759 WARN [BeanSchemaBinding] You should use the 2.0 version of the Microcontainer xml. xmlns='urn:jboss:bean-deployer:2.0'
      22:27:49,480 INFO [LocalTxDataSource] Bound datasource to JNDI name 'java:/DefaultDS'
      22:27:50,441 INFO [LocalTxDataSource] Bound datasource to JNDI name 'java:/SimpleTestTestDatasource'
      22:27:51,462 INFO [Ejb3Deployment] EJB3 deployment time took: 971
      22:27:52,534 INFO [Ejb3Deployment] EJB3 deployment time took: 1072
      22:27:52,664 INFO [MCKernelAbstraction] installing bean: jboss.j2ee:jar=jboss-seam,name=Dispatcher,service=EJB3 with dependencies:
      22:27:53,125 INFO [EJBContainer] STARTED EJB: org.jboss.seam.core.Dispatcher ejbName: Dispatcher
      22:27:53,225 INFO [MCKernelAbstraction] installing bean: jboss.j2ee:jar=jboss-seam,name=TransactionListener,service=EJB3 with dependencies:
      22:27:53,335 INFO [EJBContainer] STARTED EJB: org.jboss.seam.core.TransactionListener ejbName: TransactionListener
      22:27:53,385 INFO [Lifecycle] starting up: org.jboss.seam.servlet.exceptionFilter
      22:27:53,395 INFO [Lifecycle] starting up: org.jboss.seam.captcha.captchaImage
      22:27:54,877 INFO [Lifecycle] starting up: org.jboss.seam.servlet.multipartFilter
      22:27:54,877 INFO [Lifecycle] starting up: org.jboss.seam.servlet.redirectFilter
      22:27:54,877 INFO [Initialization] done initializing Seam
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.begin() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.end() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.begin() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Invoking @BeforeMethod org.jboss.seam.mock.SeamTest.begin()
      [Invoker 6889270] Invoking simpletest.SimpleTest.theTest
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.end() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Invoking @AfterMethod org.jboss.seam.mock.SeamTest.end()
      [Invoker 6889270] Keeping method org.jboss.seam.mock.SeamTest.cleanup() for class [TestClass class simpletest.SimpleTest]
      [Invoker 6889270] Invoking @AfterClass org.jboss.seam.mock.SeamTest.cleanup()
      22:27:54,907 INFO [Ejb] stopping the embedded EJB container
      
      *********** INVOKED METHODS
      
       org.jboss.seam.mock.SeamTest.init() 5822128
       simpletest.SimpleTest.theTest() 5822128
       org.jboss.seam.mock.SeamTest.cleanup() 5822128
      
      ***********
      
      Creating C:\Projects\SimpleTest\test-output\Test\TheSimpleTest.html
      FAILED: theTest
      java.lang.NullPointerException
       at simpletest.SimpleTest.theTest(SimpleTest.java:15)
      ... Removed 21 stack frames
      


      Any ideas?

      Thanks in advance.

      /rag