- As per Google instructions, make sure appengine-testing.jar, appengine-api-labs.jar, and appengine-api-stubs.jar are on your classpath
- Setup a datastore for testing, again just as in Googles instructions
- Run your test using a PersistenceManager just as you would normally (assuming you have a PMF setup as per Google's example)
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; import com.google.appengine.tools.development.testing.LocalServiceTestHelper; public class MyTestClass { private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()); @Before public void setUp() { helper.setUp(); } @After public void tearDown() { helper.tearDown(); } @Test public void simpleJdo() { MyObject t = new MyObject("test"); PersistenceManager pm; //prove MyObject doesn't span tests pm = PMF.getPm(); boolean notFound = false; try { pm.getObjectById(MyObject.class, "test"); fail("should have raised not found"); } catch (JDOObjectNotFoundException e) { notFound = true; } finally { pm.close(); } assertTrue(notFound); pm = PMF.getPm(); try { pm.makePersistent(t); } finally { pm.close(); } pm = PMF.getPm(); try { t = pm.getObjectById(MyObject.class, "test"); } finally { pm.close(); } assertNotNull(pm); assertEquals("test", t.getName()); } } - Yay!
Thursday, October 21, 2010
Google AppEngine JUnit tests that use JDO persistence
Google AppEngine has a very clear article on setting up local unit tests. It even says how to setup Datastore tests. What it doesn't say explicitly is that this means JDO will auto-magically work too. Naive idiots (self) therefore assume there is some magic incantation that makes it work. Or that it just doesn't work, but that doesn't really seem likely. To further confuse us, the internets abound with complex solutions for getting a PersistenceManager at test-time. Ignore all the noise; it is as simple as setting up the Datastore and just using JDO as normal. In more detail:
Subscribe to:
Post Comments (Atom)
2 comments:
Just what I was experiencing. Thanks for ending my frustration!
Thanks for clearing this up. I was getting confused by the workarounds people were coming up with. It's good to know that the LocalServiceTestHelper class loaded with the LocalDatastoreServiceTestConfig configuration can setup and teardown an in-memory datastore that is also used by the persistence manager layer.
Post a Comment