How to resolve “You have not started an Objectify context” in JUnit?

后端 未结 5 785
旧巷少年郎
旧巷少年郎 2021-01-17 16:55

I\'ve got some Objectify test code running in JUnit and I\'m getting this error:

java.lang.IllegalStateException: You have not started an Objectify context.          


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 17:54

    Jeff Schnitzer answered this here: https://groups.google.com/forum/#!topic/objectify-appengine/8HinahG7irg. That link points to https://groups.google.com/forum/#!topic/objectify-appengine/O4FHC_i7EGk where Jeff suggests the following quick and dirty workaround:

    • My @BeforeMethod starts an objectify context (ObjectifyService.begin())

    • My @AfterMethod closes the objectify context

    Jeff suggests we use ObjectifyService.run() instead but admits it's more work.

    Here's how my implementation looks:

    public class DownloadTaskRepositoryImplTest {
        // maximum eventual consistency (see https://cloud.google.com/appengine/docs/java/tools/localunittesting)
        private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
                .setDefaultHighRepJobPolicyUnappliedJobPercentage(100));
    
        private Closeable closeable;
    
        @Before
        public void setUp() {
            helper.setUp();
            ObjectifyRegistrar.registerDataModel();
            closeable = ObjectifyService.begin();
        }
    
        @After
        public void tearDown() {
            closeable.close();
    
            helper.tearDown();
        }
    

提交回复
热议问题