Maven EAR模块和EJB依赖性测试 [英] Maven EAR module and EJB dependencies tests

查看:134
本文介绍了Maven EAR模块和EJB依赖性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在建设我们的EAR& EJB项目与maven。它将构建所有EJB项目,然后将它们用作EAR的依赖项,因此最终将它们打包到EAR文件中。



问题是每个EJB项目有junit测试检查EJB。现在这些测试并不是非常有用,因为它们尝试连接到应用服务器(jboss)并从EJB接口执行方法。



有没有办法构建EJB? ,构建和部署EAR,然后从应用程序服务器运行所有EJB的所有测试?



现在我正在通过启动EJB-实现类和手动注入注入(someEJBImpl.em = EntityManager ....)这是非常讨厌的,因为我们之间有很大的依赖关系,我必须自己处理事务。



有没有其他方式对真正的AP运行EJB测试?每个EJB模块之后可能会部署EAR,并且已经构建了部分EJB模块?但是如何?



可以设置为运行所有EJB模块的Maven测试作为EAR测试的一部分?如何做?

解决方案

这不是一个简单的问题,没有简单的答案。希望这些指针会有所帮助。



我认为你最好的策略是把你的测试分成真正的单元测试 - 那些可以在没有容器的情况下孤立运行,



您可以使用 Ejb3unit ,以最大化不需要容器运行的测试。它有助于模拟一些复杂的依赖关系。 Ejb3unit有一个Maven插件,有关连接到他们的Maven存储库的详细信息,请参阅文档



其他嘲弄的框架,如JMock也可以帮助。如果您使用 ClassImposteriser ,您可以模拟课程和界面。



对于那些需要EJB容器的测试,您可以将其配置为以集成测试,根据您的EJB项目之间的关系,将它们移动到单独的项目可能是有意义的。



可以启动嵌入式Jetty实例您的JUnit测试并以编程方式向其中添加servlet。当然Jetty不是一个EJB容器,你需要一个EJB容器,如 OpenEJB



要将OpenEJB配置为Jetty,请使用如下配置:

 插件> 
< 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>

OpenEJB的依赖关系声明为:

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

您还可以使用 Selenium 帮助功能测试(假设你有这么远),这里是一个引导使用Selenium,Jetty和OpenEJB 这样做。


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 ?

解决方案

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.

这篇关于Maven EAR模块和EJB依赖性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆