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:
  1. As per Google instructions, make sure appengine-testing.jar, appengine-api-labs.jar, and appengine-api-stubs.jar are on your classpath
  2. Setup a datastore for testing, again just as in Googles instructions
  3. 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());
     }
    }
    
  4. Yay!

9 comments:

Anonymous said...

Just what I was experiencing. Thanks for ending my frustration!

Tim H said...

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.

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

Sivanandhana Girish said...

Have been a long wait for this blog. Happy on reading. Do share more of this kind.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar

online idea lab said...

Thank you for excellent article.You made an article that is interesting.
AWS Solutions Architect courses in Bangalore with certifications.
https://onlineidealab.com/aws-training-in-bangalore/

Unknown said...


Panduan Cara Bermain Judi Online Blackjack Terpercaya
Bagaimana Cara Bermain Casino Online Bagi Pemula
Cara Menang Judi Bola Sbobet
Cara Mudah Mendapatkan Kemenangan Dalam Bermain Judi Bola Maxbet
Trik Cara Bermain Taruhan Sicbo Online

john said...

Great Article android based projects

Java Training in Chennai

Project Center in Chennai

Java Training in Chennai

projects for cse

The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

tata spare parts said...

Finding the spare parts that will be fiction as the original is sometimes tricky. There are various manufacturers of auto spare parts. But to pamper your automobile, you should trust the best spare parts manufacturer. Whether it is the Spare Parts of Tata 207 or any other model, you should go for the best. Connect to us to find the collection of all Tata spare parts, which are also best in quality. Find us at www.bpautosparesindia.com/tata-spare-parts

Leyland Parts said...

Not satisfied with the spare parts supplier suggested by your friend? Rest Assured and visit BP Auto Spares India. The company deals in replacement spares and Leyland Parts. Visit the company and make your purchase order accordingly as this one supplies genuine spare parts and does not compromise quality. The products catalog and the company details are available on the company website. Also, you can call them up directly. Possibly, you will be able to recommend them to your friend next time!

Post a Comment