Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven EAR module and EJB dependencies tests

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 ?

like image 570
m_vitaly Avatar asked Jul 27 '09 03:07

m_vitaly


People also ask

What is Maven EAR plugin used for?

The EAR Plugin allows you to automatically generate the deployment descriptor, e.g. application. xml . This generation is customizable by the goal's parameters, see the goals description.

What is ear in Maven?

This plugin generates Java EE Enterprise Archive (EAR) file. It can also generate the deployment descriptor file (e.g. application. xml ).

How do you package war into your ear?

Assemble the web services-enabled WAR file into an EAR file. Assemble the EAR file that contains the JAR or WAR files. The EAR file can contain an enterprise bean or application client JAR files; web applications or WAR files; and metadata describing the applications or application. xml files.


2 Answers

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.

like image 105
Rich Seller Avatar answered Sep 30 '22 16:09

Rich Seller


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

Where can I find a complete Maven Cargo plugin example for EJB tests?

like image 39
mjn Avatar answered Sep 30 '22 15:09

mjn