如何针对与 Tomcat 不同的数据库运行 Spring Roo 生成的测试?
我有 Spring Roo 为我的域对象(和 DAO ITD)生成的集成测试集合。
它们似乎被固定为使用“生产”applicationContext.xml,它读取database.properties并连接到我为试验该项目而设置的MySQL数据库模式:
privileged aspect AdvertIntegrationTest_Roo_IntegrationTest {
declare @type: AdvertIntegrationTest: @RunWith
(SpringJUnit4ClassRunner.class);
declare @type: AdvertIntegrationTest: @ContextConfiguration
(locations = "classpath:/META-INF/spring/applicationContext.xml");
这样做的结果是我的演示数据库经常被填充通过这些测试发现垃圾。
我想更改配置,以便集成测试使用 in-mem 数据库,并单独保留 MySQL 数据库。目前,我能看到的唯一选择是删除 Roo 注释并从现在开始自己管理这些测试,但我现在宁愿让 Roo 留在循环中。
是否可以配置我的项目,以便“mvn tomcat”和“mvn test”命令使用单独的数据库,而不破坏 Spring Roo 设置?或者也许有更好的方法来实现我想做的事情?
I have a collection of integration tests that have been generated by Spring Roo for my domain objects (and DAO ITDs).
They appear to be fixed to use the "production" applicationContext.xml, which reads the database.properties and connects to the MySQL database schema I have set up for experimenting with the project:
privileged aspect AdvertIntegrationTest_Roo_IntegrationTest {
declare @type: AdvertIntegrationTest: @RunWith
(SpringJUnit4ClassRunner.class);
declare @type: AdvertIntegrationTest: @ContextConfiguration
(locations = "classpath:/META-INF/spring/applicationContext.xml");
The outcome of this is that my demo database is frequently populated with garbage by these tests.
I'd like to change the configuration so that the integration tests use an in-mem database, and leave the MySQL database well alone. At the moment, the only option that I can see is to remove the Roo annotations and manage these tests myself from now on, but I'd rather keep Roo in the loop at the moment.
Is it possible to configure my project, so the "mvn tomcat" and "mvn test" commands use separate databases, without breaking the Spring Roo set-up? Or perhaps there is a better approach for what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
肖恩,
我也曾为同样的事情而挣扎。我最终将 applicationContext.xml 的副本放在 test/resources/META-INF/spring 中,并修改下面的行:
在属性占位符指向的“test”目录中,我放置了另一个配置 hsqldb 的database.properties。
最后,我必须有一个不同的 persistence.xml 副本,它配置 SQL 方言(也在 applicationContext.xml 中)
我认为通过使用 pom.xml 魔法可以提供更优雅的解决方案,但目前这似乎是可以接受的给我解决方案。
汉斯
Sean,
I have struggled with the same thing. I ended up putting a copy of applicationContext.xml in test/resources/META-INF/spring and modifying the line below:
In that 'test' directory the property place holder points to, i have put another database.properties which configures hsqldb.
Finally, I had to have a different copy of persistence.xml which configures the SQL Dialect (also in applicationContext.xml)
I suppose that a more elegant solution through use of pom.xml magic is possible, but for now this seemed like an acceptable solution to me.
Hans
肖恩,
我遇到了同样的问题,并发现了另一件事可能对所有人都有用。
在 persistence.xml 中,可以定义多个具有不同名称的持久性单元,例如:
然后在您的 applicationContext.xml (这个用于测试)中只需要 2 处更改:
属性文件从 META-INF/spring-test/ 加载*
persistenceUnitName 指向 persistance.xml 中的“testPersistenceUnit”
希望这会对某人有所帮助,因为那里有很多答案,但很难找到
中定义多个 persistenceUnit
您可以在一个persistence.xml
Sean, all
I had the same problem and found one more thing which might be useful to all.
In persistence.xml one can define multiple persistence-unit with different names like:
Then in your applicationContext.xml (this one for tests )only 2 changes are needed:
properties files are loaded form META-INF/spring-test/*
persistenceUnitName points to "testPersistenceUnit" in persistance.xml
Hope this will help someone as there is many answers there but it is hard to find out
that you can have multiple persistenceUnits defined in one persistence.xml
Szymon
对我来说,这些步骤工作得很好:
1)在您的
src/main/resources/META-INF/persistence.xml
中添加一个新的持久性单元以用于测试目的:2)复制文件
applicationContext .xml
和database.properties
从 src/main/resources/META-INF/spring 到 src/test/resources/META-INF/ spring (如果该文件夹不存在,则创建它)。3) 将 src/test/resources/META-INF/spring/database.properties 的内容替换为如下内容:
4) 重命名文件 src/test/resources/META- INF/spring/applicationContext.xml 从
applicationContext.xml
到testApplicationContext.xml
并以如下方式更改其内容(只需更改 database.test 中的数据库引用)以及从persistenceUnit
到persistenceUnitTest
的persistenceUnitName
属性值)5) 最后,您可以像这样测试您的类:
For me these steps worked fine:
1) Add a new persistence unit in your
src/main/resources/META-INF/persistence.xml
for your test purpose:2) Copy the files
applicationContext.xml
anddatabase.properties
fromsrc/main/resources/META-INF/spring
tosrc/test/resources/META-INF/spring
(if this folder does not exist create it).3) Replace the content of the
src/test/resources/META-INF/spring/database.properties
in something like this:4) Rename the file
src/test/resources/META-INF/spring/applicationContext.xml
fromapplicationContext.xml
totestApplicationContext.xml
and change its content in something like this (simply change database references in database.test and thepersistenceUnitName
property value frompersistenceUnit
topersistenceUnitTest
)5) Finally you can test your class like this: