How to define two units of duration (one for production, one for unit testing)?

This is my runny bean:

@Stateless public class Finder { @PersistenceContext(unitName = "production") EntityManager em; [...] } 

It explicitly determines that the name of the storage unit is maintained by production . This block is configured in persistence.xml , and everything is in order. When I test this class, I have to use a different save module, with a different set of properties and configuration parameters. How do I organize it? Create another <persistence-unit> element in persistence.xml ? Is there any best practice for this?

+7
java jpa
source share
4 answers

I use the same constant unit name, but different persistence.xml files (how are you going to automate testing if you need to edit the code to enable "test mode"?).

If you are still using Maven, Maven supports natively testing versions of configuration files:

  • "production" persistence.xml is under src/main/resources/META-INF
  • "test" persistence.xml is located under src/test/resources/META-INF and is used for testing.
+8
source share

I just created another <persistence-unit> element in persistence.xml .

+2
source share

In the configuration, you can create a second persistence block, but that does not necessarily mean that you should. Of course, correct and correct combinations with several PU files, but I avoid mixing them when they are specially designed for different environments, for example. production versus dough.

In your case, I will have two save configuration files, and you have an Ant / Maven / build tool that copies / renames the correct one when necessary.

0
source share

I had a similar problem and want to suggest a different approach. I wanted to run the prod test, but I do not use two modifications of persistence.xml or code. I have only one persistence unit, but different runtimes (standalone.xml, Wildfly). Say I want to run my development database, I run Wildfly with a test runtime. When I want to simulate it as a real user, I run against the prod environment. The only difference is the record of the data source in the standalone.xml file. The descriptor is always the same (for example, myappDS, therefore the declaration of the storage unit in the persistence.xml file is always the same), but on the test server the data source record points to my test database, in prod the data source record points to my prod database.

0
source share

All Articles