mercredi 27 avril 2016

How to employ a @PersitenceContext according to the environment

I'm working on a fresh project. And I was wondering if we could annotate - or extend the annotation @PersistenceContext - of the EntityManager object, so that it would, depending on the environment, decided/inject autonomously on the persistence source.

The object call would be like: 1) A business class under test (UuT) calls a repository class, which injects an entity manager e.g. with @PersistenceContext. The entity manager would denn either grab the data from a RDBMS or a flat XML file (as used in DBUNIT).

Decision means: - if in a container (integration, production), then use the database specified in persistence.xml - if running from a unit test, then use an xml file to populate the data returned.

I currently just don't know how what the hint is for the annotation. If it's a unit test that is calling it or it is container-embedded when called.

Why do I think of this as an approach i.e. benefit I'm hoping to retrieve? Because it would free us from mocking the database for unit tests, because the entityManager simply switches to the XML as a source. And we only need to to modify the XML data according to our expectations.

Thanks for any smart reflection on my thoughts.

Aucun commentaire:

Enregistrer un commentaire