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!

21 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

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 .

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

ui ux design school said...

Thanks for the information
https://www.uiuxdesignschool.in/

Priya said...

wonderful blog.It's very interesting to read...

C Programming course at Edukators in Coimbatore

Priya said...

wonderful blog. It's very interesting to read...

AWS Training course at Edukators in Coimbatore

Priya said...

wonderful blog. It's very interesting to read...

PHP Programming course at Edukators in Coimbatore

Priya said...

Great blog. It's very interesting to read...

R Programming course at Edukators in Coimbatore

Priya said...

Grate blog. It's very interesting to read...

Manual-Testing-Training course at Edukators in Coimbatore

Priya said...
This comment has been removed by the author.
Priya said...

Awsome blog. It's very interesting to read...

Dot Net Training course at Edukators in Coimbatore

Priya said...

Nice blog. It's very interesting to read...

React-JS-Training course at Edukators in Coimbatore

Gouse said...

"This is a great introduction for developers looking to integrate Scala with Google AppEngine! The detailed walkthrough of setting up JUnit tests with JDO persistence provides valuable insights for those new to working with these technologies. I especially appreciate the clear examples and practical tips on overcoming potential hurdles. Would love to see more content on advanced testing strategies or how to optimize performance when using Scala on AppEngine!"
Digital Marketing Course In Ameerpet

shivam said...

those new to working with these technologies. I especially appreciate the clear examples and practical tips on overcoming potential hurdles. python course in pune

Post a Comment