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!

13 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.

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!

Florin M. said...

Please add my dofollow websites:
Dosula.com - Dofollow Social Bookmarks

Graybookmarks.com - Free Dofollow Bookmarks

Greenbookmark.com - Free Social Bookmarks

Freelinksubmissions.com - Free Link Submissions

Freedofollow.com - Free Dofollow Backlinks

Unknown said...

We’re A Leading & Trusted Directory Of The Best Online Brokers Review. Our Experience In The Industry Has Made Us Experts In Helping Traders Find The Most Suitable Brokerage Company For Them. Broker Forex Net Is An Online Portal Created For Traders To Help Them Find The Best Brokers And Strategies For Online Trading. Along With Broker Review And Login Details At Broker Forex We Offer Guest Post & Blog, Content Marketing Services, Link Building Services, And Much More. For More Details, Kindly Visit Our Website.

Oliver said...

At Market Forex Our Aim Is To Provide You With The Information Necessary To Make An Educated Choice When Finding The Right Trading Service Provider. We List A Variety Of Brokers Offering Many Different Platforms And Services. Gaining Access To A Wide Range Of The Best Online Brokers Will Allow You To Compare Them Side By Side And Make The Right Decision For Your Trading Needs.

Oliver said...

You'll understand the market a whole lot better with the data and graphic tools provided by our stock quote system. You'll understand how certain economic events can affect your investments, and be able to answer questions from stock market friends concerning when to buy or sell Sos Stocktwits . You will have access to live charts, and you can receive instant notifications as stocks change in cost.

Steven Cohen said...

Track your stocks in all major markets instantly with our unique live stock overview. See Ecdp stock live prices changes as they occur and view change details, including volume and share changes.

Peter Schiff said...

As a new forex trader you need to know what the best brokers offers and their White Label Forex Cost before you begin trading and investing, Use this guide to learn about trading costs, leverage and leverage limits, spreads and execution times, custodial services and market maker spreads .

Unknown said...

Free Forex White Label Partnership is the next step in our Forex Trading Education products. The course was designed to be modern and easy to use so that it can be learned by anyone, at any computer, while still allowing your customers to interact with you remotely.

Unknown said...

The Cheapest White Label Forex Broker is a sum of money which forex companies offer their partners for the use of their brand to sell the company's instruments on the partner's platform.

Baiju said...

I'm grateful for the valuable information you've shared. Your commitment to producing this content is truly commendable, and it doesn't go unnoticed. Thank you for your efforts!
Best Software Training Institute In Electronic City Bangalore

Post a Comment