java - 使用 Objectify 模拟框架?

标签 java google-app-engine mocking mockito objectify

是否可以将一些模拟框架与 Objectify 一起使用?

我已经尝试了以下方法,但它不起作用:

Foo mockFoo = mock(Foo.class);      
ObjectifyService.register(mockFoo.getClass());
ObjectifyUtil.get().put(mockFoo);

错误是:

java.lang.IllegalArgumentException: CGLIB$CALLBACK_0: org.mockito.internal.creation.MethodInterceptorFilter is not a supported property type.
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:184)
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:157)
    at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:123)
    at com.google.appengine.api.datastore.Entity.setProperty(Entity.java:320)
    at com.googlecode.objectify.impl.save.FieldSaver.setEntityProperty(FieldSaver.java:171)
    at com.googlecode.objectify.impl.save.LeafFieldSaver.saveValue(LeafFieldSaver.java:93)
    at com.googlecode.objectify.impl.save.FieldSaver.save(FieldSaver.java:156)
    at com.googlecode.objectify.impl.save.ClassSaver.save(ClassSaver.java:84)
    at com.googlecode.objectify.impl.Transmog.save(Transmog.java:342)
    at com.googlecode.objectify.impl.ConcreteEntityMetadata.toEntity(ConcreteEntityMetadata.java:231)
    at com.googlecode.objectify.impl.AsyncObjectifyImpl.put(AsyncObjectifyImpl.java:252)
    at com.googlecode.objectify.impl.AsyncObjectifyImpl.put(AsyncObjectifyImpl.java:229)
    at com.googlecode.objectify.impl.ObjectifyImpl.put(ObjectifyImpl.java:126)
    at org.foo.test.ApiTest.bar(ApiTest.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我是做错了什么,还是这不可能?

最佳答案

在我看来,模拟像 Objectify 这样大的框架不能依赖 Mockito。

public class ProductTest {
  @Rule
  public EmbeddedDataStore store = new EmbeddedDataStore();

  @Before
  public void register() {
    ObjectifyService.register(Product.class);
  }

  @Test
  public void accessObjectifyWithSuccess() {
    Objectify ofy = ObjectifyService.begin();
    ofy.put(new Product());
    assertEquals(1, ofy.query(Product.class).list().size());
  }
}

@Rule在测试之前加载数据存储。然后,注册产品类并运行集成测试。

因此,使用这种技术,您无需部署应用即可在 App Engine/Objectify 上运行测试。

以下代码是对数据存储规则进行编码的一种方法:

import org.junit.rules.ExternalResource;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class EmbeddedDataStore extends ExternalResource {
  private static LocalServiceTestHelper helper;

  @Override
  protected void before() throws Throwable {
    helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(),
      new LocalBlobstoreServiceTestConfig(), new LocalTaskQueueTestConfig(),
      new LocalMemcacheServiceTestConfig());
  }

  @Override
  protected void after() {
    helper.tearDown();
  }
}

关于java - 使用 Objectify 模拟框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325411/

相关文章:

Google App Engine 的 Java RDF 库?

google-app-engine - 如何在 IntelliJ IDEA 12 中重置 AppEngine 密码?

python - App Engine (python) 中的 MVC、GUI + 交互式可视化有哪些选项?

java - 在 Java 2D 中裁剪一般路径

java - maven liquibase 插件找不到 jitpack 依赖

python - 模拟来自同一个 python 模块的相对导入

javascript - 如何模拟 `angular.element`

python - Pytest:使用 python 线程在程序中模拟/修补 sys.stdin

Java:二维数组以列优先还是行优先顺序存储?

java - RMI 程序中缺少日志