12 Replies Latest reply on Jan 3, 2007 8:20 PM by jantzen

    Unit Testing Practices

    jantzen Newbie


      Is there a standard way of unit testing EJBs? My goal is to test Entity and Stateless session beans using JUnit, and without deploying them to the server.

      It looks like EJB3 Embedded is an option, but that seems like a poorly maintained and supported project (still in alpha since last year). Not to mention I have to roll my own solution choosing a subset of the Jars packaged there and somehow integrate them and their configuration files into my application, as it's just a collection of samples and Jars.

      How are other people handling this issue? Is there discussion elsewhere?

        • 1. Re: Unit Testing Practices
          Guy Coleman Newbie

          There are a few frameworks for testing EJBs:




          and I'm sure there are more hanging around the web somewhere. Out of those three MockEJB is the only one that runs outside the app server.

          • 2. Re: Unit Testing Practices
            jantzen Newbie

            Thanks for the suggestions. But, are these actually capable of testing EJB3s?

            MockEJB hasn't been updated in two years and I'm doesn't look like it supports dependency injection.

            JUnitEE is also two years out of date and doesn't work with either JUnit 3.8 or 4.1, and doesn't say what versions it is compatible with.

            Cactus also seems behind the curve, supporting only J2EE 1.3, and mentioning only EJB2. And it's massively overkill for my testing needs.

            Hopefully I'm just being dense today, but at this point it's looking like I need to roll my own in-container test runner as a servlet or Service bean. And out of container testing looks out of the question.

            • 3. Re: Unit Testing Practices
              Guy Coleman Newbie

              I agree - the state of EJB unit testing does seem to be a bit depressing. A lot of these projects are dormant.

              We're using MockEJB for some out of container testing of EJBs here, though we don't do anything too complex. It may be that it doesn't have the feature you need. A forum post indicated that somebody was working on it this summer but nothing seems to have come from that.

              Cactus is overkill for what we need and from past experience doesn't fit well with our home grown build scripts. The advantage to that kind of in-container test is it can check deployment descriptors etc.

              Let us know if you find anything more useful out there :-)

              • 4. Re: Unit Testing Practices
                Colin Goudie Newbie

                I've successfully started using the embedded EJB microcontainer to do unit testing (Using Test-NG).

                As far as I know (correct me if I'm wrong) the embedded microcontainer's kernel is what JBoss 5 is based on, so just because its in alpha doesnt mean its not maintained rather JBoss 5 is just in beta.

                I could probably give you some pointers if you are having issues getting it going. I know I had classpath related issues when I started and with the lack of documentation was a bit daunting.

                • 5. Re: Unit Testing Practices
                  Ian Hlavats Newbie


                  I've been exploring the embedded EJB3 container that ships with Seam 1.1 GA for the past few days (since it was released) and it looks promising.

                  I managed to migrate a sizable domain model from pure Hibernate to the Java persistence API using EJB3 entities.

                  My very simple TestNG test case runs fine (after much wrangling about in my database) but what's really throwing me for a loop are the loopy error messages in the log files.

                  Anybody else seen this type of stuff?

                  01:44:27,250 WARN org.jboss.kernel.plugins.deployment.xml.BeanSchemaBinding:227 - You should use the 2.0 version of the Microcontainer xml. xmlns='urn:jboss:bean-deployer:2.0'

                  Or this?

                  01:44:32,968 FATAL org.hibernate.ejb.Ejb3Configuration:124 - default.persistence.propertiesO-:-Ofalse
                  01:44:32,968 FATAL org.hibernate.ejb.Ejb3Configuration:124 - ejb3-interceptors-aop.xmlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - embedded-jboss-beans.xmlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - import.sqlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - jboss-jms-beans.xmlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - jndi.propertiesO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - log4j.dtdO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - log4j.xmlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - login-config.xmlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - META-INF/application.xmlO-:-Ofalse
                  01:44:33,062 FATAL org.hibernate.ejb.Ejb3Configuration:124 - META-INF/ejb-jar.xmlO-:-Ofalse

                  I saw some posts indicating this one had been fixed in CSV/SVN but I'm not quite sure how to put together a custom out-of-container EJB3 testing environment from odd JARs.

                  Anyone else have some luck with this?

                  Should I just ignore these messages and get on with the work?

                  It would be a shame to turn off logging on the org.hibernate.ejb.Ejb3Configuration just to turn off this perplexing FATAL message.

                  What does it mean?

                  I much prefer actually dealing with issues in my logs than simply ignoring them! :)


                  • 6. Re: Unit Testing Practices
                    Ian Hlavats Newbie

                    I found a solution to my problem.

                    It looks like the EJB3Configuration class in hibernate-all.jar in Seam 1.1 GA is outdated - I replaced this with the latest Hibernate 3.2.1 and Hibernate EntityManager 3.2.1 JARs and no more fatal messages.

                    Next up is the Microcontainer version warning...

                    • 7. Re: Unit Testing Practices
                      Ian Hlavats Newbie

                      This is done too.

                      I changed the xmlns value to "urn:jboss:bean-deployer:2.0" in the following XML files:


                      Good to go!

                      • 8. Re: Unit Testing Practices
                        jantzen Newbie

                        Hi Gommo and Tarantula,

                        I'm glad you guys got something working with Embedded JBoss. I guess I'll take a another stab at that approach. How are you bundling and running the tests with your application? In other words, where does Embedded JBoss live compared to the real server, and how do your unit tests find it?

                        Ideally, I'd have Embedded JBoss bundled in with my application so developers could check the whole project out of source control and be able to run the tests, but with 20+ jars that seems unwieldy. Also, are you running the TestNG units from Eclipse? I was unable to load configuration files for the embedded server while running JUnits from Eclipse, so if you have any suggestions there I'd appreciate it.


                        • 9. Re: Unit Testing Practices
                          Colin Goudie Newbie

                          I'm running TestNG using eclipse BUT from an ant file. Not using the eclipse plugin. I wanted it to work outside of eclipse as I also use Netbeans. (Thats a side issue but I wish Netbeans and Eclipse joined or something, eclipse is fast, I love its autobuilding etc.., but its project settings SUCK. You can't just checkout and build, you need to screw with launch files and they depend on your workspace name etc.. etc.. Very very poor) heheheh, anyway had to get that off my chest.

                          This is a condensed version of my structure


                          My main source is in src and my tests are in tests\src

                          When my test targets are run the source is compiled in build\ and also most classes are also copied to build-tests (The reason I have done this is the embedded server didn't support all things like ServiceBeans etc..) so I dont copy them over

                          tests\lib contains all the jars that come with the embedded download as well as testng jar

                          tests\META-INF\ contains my persistence.xml which is the same as the example I think that comes with the embedded download

                          tests\conf contains the conf files that come with the embedded download

                          The tricky part I had was ensuring that ONLY these paths are on the classpath

                          Hope that helps in some small way

                          • 10. Re: Unit Testing Practices
                            jantzen Newbie

                            Thanks for the help Gommo. I've been trying to get unit tests working inside of my primary development directory tree, which has created problems with finding config files, building properly etc. I'll try a separate test hierarchy like you've got and see if I make it any farther this time.

                            • 11. Re: Unit Testing Practices
                              jantzen Newbie


                              "gommo" wrote:

                              When my test targets are run the source is compiled in build\ and also most classes are also copied to build-tests (The reason I have done this is the embedded server didn't support all things like ServiceBeans etc..) so I dont copy them over

                              Are you sure Service beans don't work? According to this they do:


                              If they don't, that's a deal breaker in my case. Anybody successfully run them in the embedded server?

                              • 12. Re: Unit Testing Practices
                                jantzen Newbie

                                Okay, the documentation is wrong.

                                I finally got the embedded server to load my Entity beans, MDBs, and Stateless Session beans, but Service beans are a no go. Anybody know of a workaround?