Maven EAR module and EJB dependencies tests

删除回忆录丶 提交于 2020-01-11 02:26:06

问题


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 ?


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/1186348/maven-ear-module-and-ejb-dependencies-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!