如何定义两个持久性单元(一个用于生产,一个用于单元测试)? [英] How to define two persistence units (one for production, one for unit testing)?
问题描述
这是我的无状态bean:
This is my stateless bean:
@Stateless
public class Finder {
@PersistenceContext(unitName = "production")
EntityManager em;
[...]
}
它明确定义了名称持久性单位是生产
。此单元在 persistence.xml
中配置,一切正常。当我对这个类进行单元测试时,我必须使用另一个持久性单元,具有不同的属性和配置设置。我该如何组织它?在 persistence.xml
中创建另一个< persistence-unit>
元素?是否存在任何最佳实践?
It explicitly defines that the name of persistence unit is production
. This unit is configured in persistence.xml
, and everything is fine. When I'm unit testing this class I have to use another persistence unit, with different set of properties and configuration settings. How should I organize it? Create another <persistence-unit>
element in persistence.xml
? Does any best practice exist for this?
推荐答案
我使用相同的持久单元名称但不同的持久性。 xml
文件(如果您需要编辑代码以启用测试模式,您将如何自动化测试?)。
I use the same persistent unit name but different persistence.xml
files (how are you going to automate testing if you need to edit the code to enable the "test mode"?).
如果你还在使用Maven,Maven支持原生配置文件的测试版本:
If you're still using Maven, Maven supports natively having testing versions of configuration files:
- 生产
持久性.xml
位于src / main / resources / META-INF
- test
persistence.xml
在src / test / resources / META-INF
下,用于测试。
- the "production"
persistence.xml
goes undersrc/main/resources/META-INF
- the "test"
persistence.xml
goes undersrc/test/resources/META-INF
and is used for testing.
这篇关于如何定义两个持久性单元(一个用于生产,一个用于单元测试)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!