2 Replies Latest reply on Oct 16, 2008 9:34 AM by rickcr

    Question about unit testing EJB3?

    rickcr

      I'm working on an example lesson with JBoss5/Maven2/EJB3/JPA. I have it pretty much complete here http://learntechnology.net/content/ejb/maven-ejb3.jsp except I'm not showing testing yet (which I'd like to add).

      I started looking at the best way to test my Stateless Session Beans that pretty much serve as DAOs in my example. I basically started following the example as shown in this older blog post: http://escx.blogspot.com/2007/03/testing-ejb3-jpa-with-maven2.html

      Things seem ok, but if some of the EJBs call other EJBs it gets a bit more messy, but it's rare (at least in my real life cases) that were be a lot of chaining. However, I want to accommodate for it in my tests, so I'd appreciate the following:

      1) Is this a bad approach as shown here http://pastie.org/293219 (I left out the non pertinent things like the local interfaces).

      2) Is there a better recommended approach people use for testing EJB3/JPA?

      3) Is it better to possibly find some example that demonstrates standing up the EJB3 microcontainer and then run tests within that? (If this is a better approach can someone point to me some good examples of how to use this within JUnit(or TestNG)? I suppose for integration tests this would be a better way to go?

      Thanks for your time.

        • 1. Re: Question about unit testing EJB3?
          wolfgangknauf

          Hi,

          why don't you test your EJBs the same way your client would use them (connect to the server, get EJB, call methods)? This way, e.g your entity manager will work, you will use the same persistence.xml as the EJB.

          The approach described in http://escx.blogspot.com/2007/03/testing-ejb3-jpa-with-maven2.html seems to ignore all the server related things like security, transactions, injection and just tests the plain EJB methods. So the tests will not be like reality.

          Hope this helps

          Wolfgang

          • 2. Re: Question about unit testing EJB3?
            rickcr

             

            why don't you test your EJBs the same way your client would use them (connect to the server, get EJB, call methods)? This way, e.g your entity manager will work, you will use the same persistence.xml as the EJB.


            What's the best way to set that up? That's what I'm struggling with. Do you create JMX components to perform those tests? A JSP that allows you to click on some testing links? I want what is simple and I don't mind if the user does have to stand up the server first. Just wondering what the best approach is.

            I did think it would be nice if, as part of the build process, junit could run and perform all the tests. but that's not a necessity if there are some better/simpler ways to do things.