EJB3:如何在Junit测试期间将EJB3中的数据源作为原始POJO注入 [英] EJB3: How to inject DataSource in EJB3 during Junit testing as raw POJO

查看:134
本文介绍了EJB3:如何在Junit测试期间将EJB3中的数据源作为原始POJO注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用EJB的原始POJO API使用junit对EJB 3.0 EJB进行集成测试期间注入替代数据源的快速问题.

Hi quick question about injecting an alternative data-source during integration testing of an (EJB 3.0) EJB through its raw POJO API using junit.

我一直在将原始POJO服务转换为EJB3会话bean.这样做实际上仅意味着直接注释POJO.这些服务还伴随着现有的junit集成测试(用于检查查询实际测试数据库的方法的结果).

I have been converting raw POJO services to EJB3 session beans. To do so has really just meant annotating the POJOs directly. The services are also accompanied by existing junit integration tests (that check the result of methods that query a real test database).

其中一些服务需要直接的java.sql.Connection,因此我打算通过注入的DataSource对其进行配置.这样做的目的是使我可以将Bean直接部署到应用服务器(恰好发生在WLS上).但是,我也希望现有的集成测试能够继续工作.这些测试针对它们自己的测试数据库运行,因此我需要能够在运行集成测试时(在POJO/非容器环境中)注入测试配置.

Several of these services require a direct java.sql.Connection and so I intend to configure this through an injected DataSource. The intention of this is so that I can deploy the bean directly to an app server (WLS, as it happens). However, I also want the existing integration tests to continue to work. These tests run against their own test database so I need to be able to inject the test configuration when running the integration tests (in a POJO/ non-container environment).

问题是:

一旦我设置了EJB,就没有办法可以在不对容器进行操作的情况下覆盖注入的Bean吗?

Once I've set up my EJB, is there no way that I can override the injected bean without operating inside a container?

换一种说法,在运行原始POJO集成测试时,没有简单的方法来注入新的JNDI配置吗?

Put another way, is there no straightforward way to inject a new JNDI configuration when running the raw POJO integration tests?

一个示例服务类似于以下内容:

An example service resembles the following:

@Stateless(mappedName="MyInterface")
public class MyClassImpl implements MyInterface {
    ... 
    @Resource(name="jdbc/MyAppServerDataSourceJNDIName")
    DataSource ds;
    Connection conn;
...
}

N.B.我不打算将DataSource和Connection留在服务中,我只是想在有机地重构它之前先进行一些明智的工作.

我正在考虑的解决方案:

  1. 我有一个(非常糟糕的)想法是仅在包私有的服务中为Connection提供一个setter.这样,我的junit测试可以在执行之前设置Connection.然后,在应用服务器环境中,将使用注入的DS.虽然不漂亮.
  2. 我看了ejb3unit(BaseSessionBeanFixture)并正在考虑这一点.
  3. 我也理解我可以在junit中创建一个EJB容器并在该容器中运行.问题是,我想使用简单的junit测试并针对POJO(而不是EJB)来测试基本功能.
  4. 我知道这可以在春季完成(我是EJB的新手),并且正在考虑使用spring config连接EJB.

有很多信息,但没有具体信息(主要是JPA).尽管如此,SO上其他地方还是有一些很好的指针.

There's a lot of info out there but nothing specific (mostly JPA). Some good pointers elsewhere on SO though.

谢谢.

推荐答案

我的建议:

  1. 删除conn字段,并始终使用ds.getConnection()(和conn.close()).在单元测试环境中,您可以模拟DataSource/Connection(或提供获取并提供连接到测试数据库的真实"对象).
  2. 向EJB类添加一个setter.实际上,您可以注释注入方法,以便您的容器和单元测试环境更加接近.您声明添加二传手将是非常糟糕的",但是我不确定为什么. setter非常依赖于依赖注入的精神.我同意将setter添加到业务接口没有任何意义,但是将一个setter添加到bean类中似乎很好.
  1. Remove the conn field, and use ds.getConnection() (and conn.close()) consistently. In your unit testing environment, you can mock DataSource/Connection (or otherwise provide obtain and provide "real" objects that are connected to a test database).
  2. Add a setter to the EJB class. In fact, you can annotate the method for injection so that your container and unit test environments are even closer. You state that adding a setter would be "pretty awful", but I'm not sure why; setters are very much in the spirit of dependency injection. I agree that adding the setter to the business interface makes no sense, but it seems fine to add one to the bean class.

第2个示例:

@Resource(name="jdbc/MyAppServerDataSourceJNDIName")
public void setDataSource(DataSource ds) {
    this.ds = ds;
}

这篇关于EJB3:如何在Junit测试期间将EJB3中的数据源作为原始POJO注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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