NovatecConsulting / BeanTest

Bean Testing for Java EE Applications
http://blog.novatec-gmbh.de/unit-testing-jee-applications-cdi/
Apache License 2.0
25 stars 12 forks source link

Only handles the produced EntityManager #24

Closed hvoyer closed 9 years ago

hvoyer commented 9 years ago

A more generic approach regarding EntityManagers

Excellent framework, nevertheless it needs a couple of small changes to set it more generic. Change 1 - Move info.novatec.beantest.producers to the tests folders. Generating the entity manager should be left to the used application (more to this in point 2). Of course, the default will stay at is.

Change 2 - info.novatec.beantest.transactions.TransactionalInterceptor Instead of injecting a known entity manager. Inject an instance. This will give you access to all created entity managers. Ex @Inject Instance entityManagers;

Now on each method where we start stop the transaction we open/close for all of them. It is ugly but as you said, we are not a compliant multithreated unit test framework Ex. Iterator iterator = entityManagers.iterator(); while (iterator.hasNext()) { EntityManager em = (EntityManager) iterator.next(); EntityTransaction transaction = em.getTransaction(); try {

This will need, if necessary to change isFirstInterceptor Regards

carlosbarragan commented 9 years ago

Thanks a lot for the suggestions.

Regarding change 1, EntityManager should be left to the application, I think we could provide this and, at the same time, let the developer use the default EntityManager via an @Alternative. When we started to create the framework, we wanted to provide an "out-of-the-box" experience, that's why we decided to expose a default EntityManager (with a defined persistence unit "beanTest PU").

Regarding change 2, I think that could complicate things a lot. For example, when doing a commit, should the commit be executed on all entity managers or only on the one closing the transaction?

I know there are several Java EE applications that use 2 or more EntityManager's but this can be handled by the framework by merging the persistence units into one ("beanTestPU"). That means, the developer must register all participating Entities in the persistence.xml file.

Regards, Carlos