views:

1506

answers:

2

We are building our EAR & EJB projects with maven. It will build all the EJB projects and then they are used as the dependency for EAR, so they are packed into the EAR file eventually.

The problem is that each EJB project has junit tests that check the EJB. For now these tests are not very useful because they try to connect to application server (jboss) and execute methods from EJB interface.

Is there any way I can build the EJBs, build and deploy the EAR and then run all the tests from all of the EJBs against the application server ?

For now I'm simulating AP in tests by initiation EJB-Implementation classes and manually "injecting" injections (someEJBImpl.em = EntityManager....) which is very annoying, because we have a huge dependencies between them and I have to handle transactions by myself.

Is there any other way of running EJB tests against real AP ? May be deploy EAR after each EJB module with subset of EJB modules that were already built ? But how ?

May be set to run maven tests of all EJB modules as part of EAR tests ? How to do this ?

+4  A: 

This is not a simple problem, and there's no easy answer. Hopefully these pointers will help.

I think your best strategy is to separate your tests into the genuine unit tests - those that can run in isolation without a container, and move the tests that require the container into integration tests.

You can use Ejb3unit to maximise the tests that don't require a container to run. It helps mock some of the complicated dependencies. Ejb3unit has a Maven plugin, see the documentation for details connecting to their Maven repository.

Other mocking frameworks such as JMock can also help. You can mock classes as well as interfaces if you use a ClassImposteriser.

For those tests that do need an EJB container, you can configure these to run as integration tests, it may make sense to move them to a separate project, depending on the relationships between your EJB projects.

It is possible to launch an embedded Jetty instance in your JUnit tests and programmatically add servlets to it. Of course Jetty isn't an EJB container, You'll need an EJB container like OpenEJB.

To configure OpenEJB into Jetty, use a configuration like this:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <configuration>
    <scanIntervalSeconds>5</scanIntervalSeconds>
    <contextPath>/example</contextPath>
    <systemProperties>
      <systemProperty>
        <name>java.naming.factory.initial</name>
        <value>org.apache.openejb.client.LocalInitialContextFactory</ value>
      </systemProperty>
      <systemProperty>
        <name>java.naming.factory.url.pkgs</name>
        <value>org.mortbay.naming</value>
      </systemProperty>
    </systemProperties>
  </configuration>
</plugin>

The dependency declarations for OpenEJB would be:

<dependency>
  <groupId>org.apache.openejb</groupId>
  <artifactId>openejb-core</artifactId>
  <version>3.1</version>
  <scope>test</scope>
</dependency>

You can also use Selenium to help with the functional tests (assuming you got this far), here's a guide using Selenium, Jetty and OpenEJB to do so.

Rich Seller
+1  A: 

For JBoss you could try the Maven Cargo plugin. I am currently testing it with JBoss 5.1 and still working on it:

http://stackoverflow.com/questions/1707740/where-can-i-find-a-complete-maven-cargo-plugin-example-for-ejb-tests

mjustin