On Jueves 25 abril 2013 18:19:07 Marian Seitner escribió:
Perhaps you should enclose each test in a transaction, like in the @Before and
@After
methods in this JPA test base class:
public abstract class JpaTestBase {
public static void setUpJpa(Module module, List<Class<?>> finders) {
final JpaPersistModule persistModule = new
JpaPersistModule("ofertas-test");
for (final Class<?> finder : finders) {
persistModule.addFinder(finder);
}
final Module common = new Module() {
@Override
public void configure(Binder binder) {
final Properties jpaProperties = new Properties();
jpaProperties.put("jdbc.batchSize", "50");
Names.bindProperties(binder, jpaProperties);
}
@Provides
@JdbcBatchSize
Integer getBatchSize(@Named("jdbc.batchSize") Integer batchSize) {
return batchSize;
}
};
final List<Module> list = Lists.newArrayList();
list.add(common);
list.add(persistModule);
list.add(module);
// modules to array and create Guice
final Module[] arr = list.toArray(new Module[list.size()]);
JpaTestBase.injector = Guice.createInjector(arr);
// make sure we get our dependencies
JpaTestBase.service =
JpaTestBase.injector.getInstance(PersistService.class);
JpaTestBase.unitOfWork =
JpaTestBase.injector.getInstance(UnitOfWork.class);
JpaTestBase.emp = JpaTestBase.injector.getProvider(EntityManager.class);
// NB: we need to start the service (injected)
JpaTestBase.service.start();
}
@Before
public void setupTransaction() throws Exception {
JpaTestBase.emp.get().getTransaction().begin();
}
@After
public void cleanTransaction() throws Exception {
if (JpaTestBase.emp.get().getTransaction().getRollbackOnly()) {
JpaTestBase.emp.get().getTransaction().rollback();
} else {
JpaTestBase.emp.get().getTransaction().commit();
}
}
@AfterClass
public static void tearDownJpa() {
JpaTestBase.service.stop();
}
protected static Provider<EntityManager> emp;
protected static PersistService service;
protected static UnitOfWork unitOfWork;
protected static Injector injector;
}
> Hi,
>
> I'm working on an integration test for a Java SE application.
>
> Here's what I'm basically doing:
>
> *# Phase 1:*
>
> Injector i1 = Guice.createInjector(..., new JpaPersistModule("myUnit")); //
> all done in my module, but nothing fancy here
>
> // bootstrap some instances
> PersistService persistService = i1.getInstance(PersistService.class);
> UserService service = i1.getInstance(UserService.class);
>
> // start persist
> persistService.start();
>
> // populate database
> service.createUser(foo, bar);
> ...
>
> // stop persist
> persistService.stop;
> i1 = null;
>
> *# Phase 2:*
>
> Injector i2 = Guice.createInjector(..., new JpaPersistModule("myUnit")); //
> all done in my module, but nothing fancy here
>
> // bootstrap some instances
> PersistService persistService = i2.getInstance(PersistService.class);
> MyApplication app = i2.getInstance(MyApplication.class);
>
> // start persist
> persistService.start();
>
> // do some work
> app.doSomethingUseful(); <-- doesn't work
>
> This time I get the following Exception:
> java.lang.IllegalStateException: Attempting to execute an operation on a
> closed EntityManagerFactory.
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.verifyOpe
> n(EntityManagerFactoryDelegate.java:306)
> ~[org.eclipse.persistence.jpa-2.4.1.jar:na]
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEnt
> ityManagerImpl(EntityManagerFactoryDelegate.java:277)
> ~[org.eclipse.persistence.jpa-2.4.1.jar:na]
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityM
> anagerImpl(EntityManagerFactoryImpl.java:304)
> ~[org.eclipse.persistence.jpa-2.4.1.jar:na]
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityM
> anager(EntityManagerFactoryImpl.java:282)
> ~[org.eclipse.persistence.jpa-2.4.1.jar:na]
> at
> com.google.inject.persist.jpa.JpaPersistService.begin(JpaPersistService.jav
> a:70) ~[guice-persist-3.0.jar:na]
> at
> com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInt
> erceptor.java:49) ~[guice-persist-3.0.jar:na]
>
> After debugging it turned out that the highlighted JpaLocalTxnInterceptor
> got the JpaPersistService from phase 1 injected, which is (of course)
> already closed.
>
> Any idea what happended here? I "fixed" it by removing the
> persistService.stop() call in phase 1, but I'm not sure if this behaviour
> is to be expected...
>
> Thanks,
> Marian
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.