该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-03-07
源文转自:More on unit testing with an injected JDO PersistenceManager
Regarding my previous post, it turns out that I needed a TestPMF implementation sooner than I thought. The reason is the way I’m injecting a DispatchTestService in my unit tests (as described in this post). I’m calling createInjector() in the setUp() method, which gets run before each test method:
@Override protected void setUp() throws Exception { super.setUp(); Injector inj = Guice.createInjector(new ServerModule(), new DispatchTestModule()); testSvc = inj.getInstance(StandardDispatchService.class); // This pm is needed only for code in this class that calls a PM directly // ActionHandlers are injected with the PMF singleton from Guice pm = inj.getInstance(PMF.class).getPersistenceManager(); }
Since setUp() gets called before each test method, Guice is initialized for each test method, and therefore Guice calls the constructor for my DefaultPMF class for each test method. Repeated from the previous post, the DefaultPMF class looks like:
package com.turbomanage.gwt.server; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; public final class DefaultPMF implements com.turbomanage.gwt.server.PMF { private final PersistenceManagerFactory pmfInstance = JDOHelper .getPersistenceManagerFactory("transactions-optional"); public DefaultPMF() { } @Override public PersistenceManager getPersistenceManager() { return pmfInstance.getPersistenceManager(); } }
Because DefaultPMF creates a named PersistenceManagerFactory, JDO complains that the named PersistenceManagerFactory has already been created. My solution for now is to replace the DefaultPMF with a TestPMF that uses a Properties map to initialize the PersistenceManager, just as the AppEngineFan TestInitializer does. Here’s a working TestPMF:
package com.turbomanage.gwt.server; import java.util.Properties; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; public class TestPMF implements PMF { private final PersistenceManagerFactory pmf; public TestPMF() { Properties newProperties = new Properties(); newProperties .put("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"); newProperties.put("javax.jdo.option.ConnectionURL", "appengine"); newProperties.put("javax.jdo.option.NontransactionalRead", "true"); newProperties.put("javax.jdo.option.NontransactionalWrite", "true"); newProperties.put("javax.jdo.option.RetainValues", "true"); newProperties.put("datanucleus.appengine.autoCreateDatastoreTxns", "true"); newProperties.put("datanucleus.appengine.autoCreateDatastoreTxns", "true"); pmf = JDOHelper.getPersistenceManagerFactory(newProperties); } @Override public PersistenceManager getPersistenceManager() { return pmf.getPersistenceManager(); } }
Now simply bind PMF to its TestPMF implementation in your Guice module for tests (DispatchTestModule in the setUp() method above), and each unit test method will run with a freshly created PersistenceManager. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 1000 次