如何解决“您尚未开始Objectify上下文”在JUnit中?

编程入门 行业动态 更新时间:2024-10-21 11:49:52
本文介绍了如何解决“您尚未开始Objectify上下文”在JUnit中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些在JUnit中运行的Objectify测试代码,并且出现这个错误:

java.lang。 IllegalStateException:您尚未启动Objectify上下文。您可能错过了ObjectifyFilter。如果您没有在http请求的上下文中运行,请参阅ObjectifyService.run()方法。 at com.googlecode.objectify.ObjectifyService.ofy(ObjectifyService.java:44) at com.googlecode.objectify.impl.ref.LiveRef。< init>(LiveRef.java:31) at com.googlecode.objectify.Ref.create(Ref.java:26) at com.googlecode.objectify.Ref.create(Ref.java:32) at combase。 followerdownloader.repository.DownloadTaskRepositoryImpl.create(DownloadTaskRepositoryImpl.java:35) at combase.followerdownloader.repository.DownloadTaskRepositoryImplTest.setUp(DownloadTaskRepositoryImplTest.java:45)

如何解决这个问题的测试代码?

解决方案

Jeff Schnitzer在这里回答了这个问题: groups.google/forum /#!话题/客观化-的AppEngine / 8HinahG7irg 。该链接指向 groups.google/forum/# !topic / objectify-appengine / O4FHC_i7EGk Jeff在其中建议了以下快速和肮脏的解决方法:

  • My @BeforeMethod开始一个客观化上下文(ObjectifyService.begin())

  • My @AfterMethod关闭客观化上下文

  • ul>

    Jeff建议我们使用 ObjectifyService.run()代替它,但承认它有更多的工作。

    下面是我的实现的样子:

    public class DownloadTaskRepositoryImplTest { //最大一致性(请参阅cloud.google/appengine/docs/java/tools/localunittesting) private final LocalServiceTestHelper helper = $ b $ new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() .setDefaultHighRepJobPolicyUnappliedJobPercentage(1 00)); 私密可关闭; @Before public void setUp(){ helper.setUp(); ObjectifyRegistrar.registerDataModel(); closeable = ObjectifyService.begin(); } @ 之后public void tearDown(){ closeable.close(); helper.tearDown(); }

    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. You are probably missing the ObjectifyFilter. If you are not running in the context of an http request, see the ObjectifyService.run() method. at com.googlecode.objectify.ObjectifyService.ofy(ObjectifyService.java:44) at com.googlecode.objectify.impl.ref.LiveRef.<init>(LiveRef.java:31) at com.googlecode.objectify.Ref.create(Ref.java:26) at com.googlecode.objectify.Ref.create(Ref.java:32) at combase.followerdownloader.repository.DownloadTaskRepositoryImpl.create(DownloadTaskRepositoryImpl.java:35) at combase.followerdownloader.repository.DownloadTaskRepositoryImplTest.setUp(DownloadTaskRepositoryImplTest.java:45)

    How do I resolve this for test code?

    解决方案

    Jeff Schnitzer answered this here: groups.google/forum/#!topic/objectify-appengine/8HinahG7irg. That link points to groups.google/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 cloud.google/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(); }

更多推荐

如何解决“您尚未开始Objectify上下文”在JUnit中?

本文发布于:2023-10-25 08:44:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   如何解决   您尚未   Objectify   JUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!