2 Replies Latest reply on Jun 8, 2004 5:35 AM by Darran Lofthouse

    How to test my J2EE app with JUnit

    null null Newbie

      Hello all,

      I have a set of classes which use Hibernate as a persistance layer. Hibernate is set up to use a Datasource, which is configured and deployed to my JBoss 3.2 server. I deploy my app and it all works great - doesnt do much yet but I can query the database and it presents results to the screen.

      I want to add logic to my classes now and want to use test driven development, but I want my domain classes to have the Hibernate layer functioning as well for testing the database logic. The trouble is when I run JUnit, my Hibernate based classes cannot access all the JBoss JNDI resoruces. I've set the JNDI properties in my JUnit VM to look at the JBoss JNDI context but it can only see the Global entries, not the "java:" ones. Im assuming thats because they are running in different VM's and the "java:" namespace is not made available outside of the VM it is running in.

      My questions are:
      1. Can I get a JNDI resouce from JBoss into a different VM (such as a Datasource made available to JUnit) so I may test these sorts of classes properly?
      2. If not, can I deploy a datasource config so that it is deployed in a Global scope, rather than "java:" local VM scope?

      I've actually seen it working but I dont know how they did it, and it was about 18 months ago. I'm sure someone has come across this before but hours of Googling has turned up nothing :)

      Thanks,
      Jason

      Thanks,
      Jason

        • 1. Re: How to test my J2EE app with JUnit
          Phil Newbie

          There's an open source extention to JUnit called Cactus which you can use for testing server-side code. It's available from the apache foundation web site. it even contains tasks for ant, so you can automate your testing into you build process.

          Another option (assuming you're doing all your JNDI lookups etc from one class, which you should me because it makes your code more maintainable) is to plug in a different implementaion of that class that does not do JNDI lookups etc. for testing.

          • 2. Re: How to test my J2EE app with JUnit
            Darran Lofthouse Master

             

            1. Can I get a JNDI resouce from JBoss into a different VM (such as a Datasource made available to JUnit) so I may test these sorts of classes properly?


            JNDI can be accessed from a different JVM, however Datasources can not be accessed remotely. If you use Cactus as sugested by Carpy you will be running in the same JVM so will be able to access them.

            2. If not, can I deploy a datasource config so that it is deployed in a Global scope, rather than "java:" local VM scope?


            No