简体   繁体   English

如何在不同的持久性单元上使用JPA和JUnit进行测试?

[英]How to do tests with JPA and JUnit on differents persistence units?

I am very new using JUnit. 我是使用JUnit的新手。 I want to test some classes that implements JPA. 我想测试一些实现JPA的类。 But, I have two databases, one for testing and other for development. 但是,我有两个数据库,一个用于测试,另一个用于开发。

Normally, the testing db is clean. 通常,测试数据库是干净的。 I test inserts, updates, delets, so and so, but any time I have to switch the db connection to testing db. 我测试插入,更新,删除等等,但是任何时候我必须将数据库连接切换到测试数据库。

How can I have two persistence units: one for testing and other for development? 我如何有两个持久性单元:一个用于测试,另一个用于开发?

Thanks... 谢谢...

You can either have two different persistence units in your persistence.xml file, or you can have one, and create your EntityManagerFactory passing in a properties map that override the database parameters. 您可以在persistence.xml文件中有两个不同的持久性单元,也可以有一个,然后通过传递覆盖数据库参数的属性映射来创建EntityManagerFactory。

Your entities/mappings can be the same for both. 两者的实体/映射可以相同。

您需要使用2套spring config文件或配置类,并在单元测试中引用测试配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM