5 Replies Latest reply on May 21, 2007 12:19 PM by jfrankman

    Test EJB3/Seam from eclipse

    jfrankman Novice

      If I should be asking this in another forum, please let me know.

      I am slowly coming up to speed with seam and really enjoy it. But I cannot figure out how to test or execute my data access layer unless it is running inside of JBoss. Before with struts, I did not have EJB, just plain POJOs and Hibernate xml mappings, so I could test my methods (i.e. daoClassA.findById() ) right from eclipse either as unit test or even using a small test class with println statements without deploying to JBoss. But now my DAO's get the TransactionManager from Seam and the database mapping are annotations in my POJOs instead of xml files.

      This may seem like a stupid question to seasoned Java developers, but I am just getting started. Can someone explain if there is a way to run or test my DAO and mapped pojos from inside Eclipse?

      I have been looking into an "embeddable container" to run the tests. Am I on the right track?

        • 1. Re: Test EJB3/Seam from eclipse
          Fernando Montaño Expert

          Yes, you are in the right track. Seam provides a way you can do unit and integration (functional) tests.

          See: http://docs.jboss.com/seam/latest/reference/en/html/testing.html

          • 2. Re: Test EJB3/Seam from eclipse
            Stuart Robertson Novice

            If you used seamgen to create your project (highly recommended), that will include the embedded ejb microcontainer. You can import it directly into eclipse as a java project (look for wtp-style projects once the new ide comes out apparently - until then don't try, nearly impossible).

            Anyway, from there you can extend SeamTest as described at length in the documentation. I think they even create an example test for you when you use seamgen. I have unit tests for daos, SFSB, etc which run very well using TestNG.

            • 3. Re: Test EJB3/Seam from eclipse
              jfrankman Novice

              Thanks for your replies. I did use seam gen, so I will take a look at it.

              I have one more question about the documentation you pointed me at. How does the Local Entity Manager get declared and created? This is normally done with the persistence.xml files with JNDI, right? But how is it done locally inside Eclipse? In the documentation it shows this:

              @Configuration(beforeTestClass=true)
               public void init()
               {
               emf = Persistence.createEntityManagerFactory("myResourceLocalEntityManager");
               }


              In the code snippet above where and how is "myResourceLocalEntityManager" declared/created? Is it in an xml file config file, or something else?

              • 4. Re: Test EJB3/Seam from eclipse
                jfrankman Novice

                I will answer my own question concerning my "emf = Persistence.createEntityManagerFactory("myResourceLocalEntityManager");" question.

                I just had to define the persistence-unit in a persistence.xml file.

                <persistence-unit name="sample">
                 <provider>org.hibernate.ejb.HibernatePersistence</provider>
                 <class>com.idfbins.nexus.common.vo.busent.FBWorker</class>
                 <properties>
                 <property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver"/>
                 <property name="hibernate.connection.username" value="db2admin"/>
                 <property name="hibernate.connection.password" value="db2admin"/>
                 <property name="hibernate.connection.url" value="jdbc:db2://nexusdbtest:50001/nxsprot1"/>
                 <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
                
                 </properties>
                 </persistence-unit>


                If I understand this correctly, EJB3 requires the persistence.xml file to exist in the META-INF directory. Seam-gen creates three separate persistence files: persistence-dev.xml, persistence-test.xml, and persistence-prod.xml. It then copies one of these into the META-INF directory from the ANT build.

                When I tried to place the persistence-unit into the persistence-*.xml files nothing worked. I am sure this could be fixed in the build.xml file.
                But, for me to get this working from Eclipse I had to create a persistence.xml file and place it in the META-INF directory of my project. I then included the persistence-unit shown in the xml above. Also note that I had to declare the mapped class using the class tag "com.idfbins.nexus.common.vo.busent.FBWorker
                ". I do not know if I have to do this for every mapped POJO or if there if I can just specify a package name and/or wildcard to define all mapped classes in my project.

                These two links helped me figure this out:
                http://trailblazer.demo.jboss.com/EJB3Trail/persistence/config/
                http://www.censnet.it/articles/cap03.htm

                • 5. Re: Test EJB3/Seam from eclipse
                  jfrankman Novice

                  I am having one more issue. Any ideas or explanations are appreciated. I can now get the entitymanager from inside eclipse. I can call the EntityManager's persist and find methods directly in my test case but I cannot successfully call my service and DAO layer classes. I am trying to test a service layer class, but since I am injecting the dao into the service (see code snippet below), when I run the test from Eclipse the DAO used by the service class is null. I would have thought that the embedded container would have injected this in for me.

                  My ecllipse project was created by seam-gen and I do have a source folder: "embedded-ejb/conf" in my project. I have not modified any of the config files. Is there anything else I need to do to get the embedded container to work inside eclipse such as modifying the embedded container config files or adding any jars to the classpath? If not, am I barking up the wrong tree? Is it possible to test the service and dao layers using the embedded container?


                  @Stateless
                  @Name("clientService")
                  public class ClientServiceImpl implements ClientService
                  {
                   @In(create=true) private ClientDAO clientDao;