4 Replies Latest reply on Jan 18, 2008 11:35 AM by pmuir

    Need definitive answers on unit testing with Seam

    grettke_spdr

      Hi folks,

      We are going forward with Seam as the foundation of our application architecture for "the big project". So, I need some definitive answers on unit testing with Seam.

      There is conflicting information regarding how one should go about unit testing components. One camp says to use SeamTest, the other camp says that you should not use it.

      See in particular: here and here.

      Or search for "unit test could not find datasource" with "include all items" set to true in the Seam forum.

      Q1. What is the right way to unit test Seam components?

      Q2. How does one test without SeamTest?

      The same problem that everyone seems to run into is that when they make the call:

      Persistence.createEntityManagerFactory("my-data-source")
      


      The testing errors out since the datasource can not be found. The suggestion was made to register the datasource in JNDI, but no one (including myself hence this post) talks about how to do so without relying on SeamTest to do so.

      What are all the steps that one needs to take to perform unit testing against a datasource without using the embedded container?

      Ref: here.

      Q2. How does one test with SeamTest?

      Everyone seems to be getting the "cannot find datasource" error because the test gets run before the embedded container is actually initialized. The generally agreed upon workaround seem to be to initialize the datasource before every test is run:

      See: here.

      This is how we did it on our first Seam project. The code is ugly and hacking; bless the programmer that took care of it for the team.

      Is this really the "right" way to do it. I feel like I am doing something wrong and this is a workaround. What is the official way to deal with this? Is this normal behavior or are all of users doing something wrong?

      What is the official way to deal with the issue? Is it really as simple as "configure everything exactly the same way as the sample projects"? Remember everyone is encountering this error using seam-gen'd projects.

      This is really confusing. Gavin, Pete, we need your magic on this, you are the ones who can shed the light onto our ignorance. This may sound like craziness to you, but it is really frustrating.

      Looking forward to your reply! :)

        • 1. Re: Need definitive answers on unit testing with Seam
          christian.bauer

          All of the threads you link to are from people who didn't read the updated documentation of Seam 2.0:


          The Java Persistence API can be used with both Java SE and Java EE — when the above component is used inside an Application Server (Java EE) the container is responsible for transaction management, however in the unit test (Java SE) the transaction must be managed explicitly using a resource local entity manager. This requires configuration in persistence.xml.


          Note the last sentence. All the postersl (and probably you) have problems because they are using the wrong persistence.xml. If you don't understand how the persistence.xml needs to look like for a standalone EntityManager test (without SeamTest container), read the second chapter of my book (it's free).



          • 2. Re: Need definitive answers on unit testing with Seam
            christian.bauer
            • 3. Re: Need definitive answers on unit testing with Seam
              grettke_spdr

               

              "christian.bauer@jboss.com" wrote:
              All of the threads you link to are from people who didn't read the updated documentation of Seam 2.0:


              That is me, I was reading 1.2.1.GA documentation as that is what we are using right now.

              "christian.bauer@jboss.com" wrote:

              Note the last sentence. All the postersl (and probably you) have problems because they are using the wrong persistence.xml. If you don't understand how the persistence.xml needs to look like for a standalone EntityManager test (without SeamTest container),


              In other words we need to get off our lazy butts and read up on how JPA works! :)

              You put it very nicely, though. One of the guys at work would've said to me "Grant. Thinking is required.". I suppose there is just a lot on everyone's mind right now.

              Folks using seam-gen'd projects honestly expect this stuff to be wired up out of the box. Lazy people? Perhaps (finger pointing back at myself).

              "christian.bauer@jboss.com" wrote:
              read the second chapter of my book (it's free).


              I own a copy of Christian's book. It is a real treat. I wouldn't say that just because Christian is so helpful either. If it sucked I just wouldn't have mentioned it at all.

              • 4. Re: Need definitive answers on unit testing with Seam
                pmuir

                This is just a really bad example of unit testing with Seam. I replaced it with a good unit test that tests business logic (which is what you should unit test imo).