This is my stateless bean:
@Stateless
public class Finder {
@PersistenceContext(unitName = "production")
EntityManager em;
[...]
}
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?
A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store. Persistence units are defined by the persistence.xml configuration file.
xml files. Have multiple persistence units in one persistence.
I simply created another <persistence-unit>
element in persistence.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"?).
If you're still using Maven, Maven supports natively having testing versions of configuration files:
persistence.xml
goes under src/main/resources/META-INF
persistence.xml
goes under src/test/resources/META-INF
and is used for testing.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With