I'm a big fan of the J2EE Container-managed injection of the Java
Persistence API (JPA) into private fields, such as
@PersistenceUnit
private EntityManagerFactory emf;
However, when writing JUnit tests, there is no container management*
and one has to manually call Persistence.createEntityManagerFactory.
It is also necessary to programmatically set some properties such as
the test database connection, because again there is no container
management and JDNI JDBC connections are not available.
Is it possible to use Guice in the JUnit environment to provide
injected 'EntityManagerFactory's? I seen the following post
http://cowwoc.blogspot.com/2008/10/integrating-google-guice-into-junit4.html
and I am wondering if this is still the recommended way to do things.
Guice 2 has been released since this post was made in 2008. And the
following looks like it is doing much more than I need - for a start,
the EntityManagerFactory is never going to be the test subject.
http://code.google.com/p/atunit/
Thanks in advance.
* unless someone knows of a painless way to get JUnit suites and
individual tests to run inside Glassfish from NetBeans.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
If I write an application for J2EE, then the container *already*
injects the EntityManagerFactory with the following code
@PersistenceUnit
private EntityManagerFactory emf;
without any need to use Guice. I don't understand why warp-persist is
trying to do the same thing.
However, when I run 'main' applications, or JUnit tests, I can obtain
an EntityManagerFactory with a single static method call. So anything
more than a single line of code is starting to get heavyweight. warp-
persist seems to require some setup boilerplate to initialise the JPA
provider.
What I was hoping was that I could augment my JUnit tests by using the
@RunWith annotation, and then have a field in my JUnit test that looks
something like
@Inject
private EntityManagerFactory emf;
The scenario I want would be that Guice manages the life cycle of the
EntityManagerFactory, so that on shutdown (with a shutdown hook, for
example), the EntityManagerFactory is closed - which implies several
things for the database, most notably when the CREATE_AND_DROP schema
strategy is selected. It would be preferable (perhaps even necessary)
to pass a parameter to the annotation with the name of the Persistence
Unit - that would allow the definition of a test database and setup in
the persistence.xml file - different databases for testing/deployment.
e.g.
@Inject(puName = "MyTestPU")
private EntityManagerFactory emf;
On Dec 31 2009, 10:47 pm, "Dhanji R. Prasanna" <dha...@gmail.com>
wrote:
> You can check out warp-persist which is meant to do just that.
>
> http://code.google.com/p/warp-persist
>
> Dhanji.
>
>
>
> On Fri, Jan 1, 2010 at 5:54 AM, Sam Halliday <sam.halli...@gmail.com> wrote:
> > Dear all,
>
> > I'm a big fan of the J2EE Container-managed injection of the Java
> > Persistence API (JPA) into private fields, such as
>
> > @PersistenceUnit
> > private EntityManagerFactory emf;
>
> > However, when writing JUnit tests, there is no container management*
> > and one has to manually call Persistence.createEntityManagerFactory.
> > It is also necessary to programmatically set some properties such as
> > the test database connection, because again there is no container
> > management and JDNI JDBC connections are not available.
>
> > Is it possible to use Guice in the JUnit environment to provide
> > injected 'EntityManagerFactory's? I seen the following post
>
> >http://cowwoc.blogspot.com/2008/10/integrating-google-guice-into-juni...
>
> > and I am wondering if this is still the recommended way to do things.
> > Guice 2 has been released since this post was made in 2008. And the
> > following looks like it is doing much more than I need - for a start,
> > the EntityManagerFactory is never going to be the test subject.
>
> > http://code.google.com/p/atunit/
>
> > Thanks in advance.
>
> > * unless someone knows of a painless way to get JUnit suites and
> > individual tests to run inside Glassfish from NetBeans.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "google-guice" group.
> > To post to this group, send email to google...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-guice...@googlegroups.com<google-guice%2Bunsubscribe@google groups.com>