[英]Mocking db queries with querydsl - issue with Optional
I've some difficulties to write tests on DB queries for my application, which uses querydsl on top of a mongo. 我很难为我的应用程序在数据库查询上编写测试,该测试在mongo上使用querydsl。 I've found several examples of people who just unit-test the query object itself, but I'd like to go a step further and test how the query performs (like an integration test) BUT without having to start the whole DB process. 我发现了一些仅对查询对象本身进行单元测试的人的示例,但是我想更进一步,无需启动整个数据库过程即可测试查询的性能(如集成测试)。 That is, mocking the DB exclusively with java tools. 也就是说,仅使用Java工具模拟数据库。
I couldn't find anything for that, tools such as DBUnit or DbSetup need an actual connection to DB. 我找不到任何东西,诸如DBUnit或DbSetup之类的工具需要与DB的实际连接。 So I started to write my own classes, and it almost works. 因此,我开始编写自己的类,并且几乎可以正常工作。 The idea is to use com.mysema.query.collections.CollQuery and mockito to simulate a database that would receive my application's queries, with a wrapper "CollQuery to Query". 这个想法是使用com.mysema.query.collections.CollQuery和mockito来模拟一个数据库,该数据库将使用包装“ CollQuery to Query”来接收我应用程序的查询。 Basically, it works like this: 基本上,它是这样的:
public class MyServiceTest {
private MyService service;
private final Collection<MyObject> fakeTable = new ArrayList<>();
@Before
public void setup() {
final Persister persister = mock(Persister.class);
when(persister.query(any(Class.class)))
// MockedQuery is the wrapper I wrote
.thenReturn(new MockedQuery<>(QMyObject.myObject, fakeTable));
service = new MyService(persister);
}
@Test
public void shouldWork() {
fakeTable.add(new MyObject("one"));
fakeTable.add(new MyObject("two"));
fakeTable.add(new MyObject("three"));
final List<MyObject> result = service.getOne();
// service.getOne would do something like:
// persister.query(QMyObject.myObject).where(QMyObject.myObject.name.eq("one")).list()
assertThat(result).hasSize(1);
}
}
... and basically, it seems to work! ...而且基本上,它似乎可以工作! Except that my code heavily uses guava's Optional, and it seems to be a problem for querydsl. 除了我的代码大量使用guava的Optional之外,对于querydsl来说似乎是一个问题。 If, instead of a String, MyObject takes an Optional < String > , then I get the error: 如果MyObject取一个Optional <String>而不是一个String,则出现错误:
com.mysema.codegen.CodegenException: Compilation of public class Q_01363784216_1275614662_1275614662_1573163836_01698119955_566403833 {
public static Iterable<xxx.OpenedInterruption> eval(Iterable<xxx.OpenedInterruption> openedInterruption_, xxx.InterruptionType a1, xxx.InterruptionTargetType a2, com.google.common.base.Present a3) {
java.util.List<xxx.OpenedInterruption> rv = new java.util.ArrayList<xxx.OpenedInterruption>();
for (xxx.OpenedInterruption openedInterruption : openedInterruption_) {
try {
if (com.mysema.query.collections.CollQueryFunctions.equals(com.mysema.query.collections.CollQueryFunctions.<xxx.InterruptionType>get(openedInterruption, "type"), a1) && com.mysema.query.collections.CollQueryFunctions.equals(com.mysema.query.collections.CollQueryFunctions.<xxx.InterruptionTargetType>get(openedInterruption, "targetType"), a2) && com.mysema.query.collections.CollQueryFunctions.equals(com.mysema.query.collections.CollQueryFunctions.<com.google.common.base.Optional>get(openedInterruption, "target"), a3)) {
rv.add(openedInterruption);
}
} catch (NullPointerException npe) { }
}
return rv; }
}
failed.
/Q_01363784216_1275614662_1275614662_1573163836_01698119955_566403833.java:3: error: Present is not public in com.google.common.base; cannot be accessed from outside package
public static Iterable<xxx.OpenedInterruption> eval(Iterable<xxx.OpenedInterruption> openedInterruption_, xxx.InterruptionType a1, xxx.InterruptionTargetType a2, com.google.common.base.Present a3) {
^
1 error
at com.mysema.codegen.JDKEvaluatorFactory.compile(JDKEvaluatorFactory.java:74)
at com.mysema.codegen.AbstractEvaluatorFactory.createEvaluator(AbstractEvaluatorFactory.java:128)
at com.mysema.query.collections.DefaultEvaluatorFactory.createEvaluator(DefaultEvaluatorFactory.java:157)
at com.mysema.query.collections.DefaultQueryEngine.evaluateSingleSource(DefaultQueryEngine.java:176)
at com.mysema.query.collections.DefaultQueryEngine.list(DefaultQueryEngine.java:91)
at com.mysema.query.collections.AbstractCollQuery.list(AbstractCollQuery.java:219)
at xxx.BusinessInterruptionServiceImplTest$MockedQuery.list(BusinessInterruptionServiceImplTest.java:143)
at xxx.BusinessInterruptionServiceImplTest.setup(BusinessInterruptionServiceImplTest.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
So, it looks like mysema tries to instanciate com.google.common.base.Present , but is unable to do so. 因此,看起来mysema试图实例化com.google.common.base.Present,但无法这样做。 The weird part is that the same query works perfectly with my production code, so maybe the problem is very specific to the "CollQuery" class I use for mocking. 奇怪的是,同一查询与我的生产代码完美配合,因此问题可能是针对我用于模拟的“ CollQuery”类的。
Any idea about how to solve this? 关于如何解决这个问题的任何想法?
Or alternatively, if someone can see a different way to mock the DB like I'm trying to do, I would appreciate any help! 或者,如果有人能像我尝试的那样看到另一种模拟数据库的方式,我将不胜感激!
Thanks 谢谢
PS: here's my wrapper "MockedQuery" class (quite straightforward): PS:这是我的包装器“ MockedQuery”类(非常简单):
public class MockedQuery<T> implements Query<T> {
private final Path<T> path;
private final CollQuery collQuery;
public MockedQuery(final Path<T> path, final Iterable<T> collection) {
this.path = path;
collQuery = from(path, collection);
}
@Override
public boolean exists() {
return collQuery.exists();
}
@Override
public boolean notExists() {
return !exists();
}
@Override
public CloseableIterator<T> iterate() {
return collQuery.iterate(path);
}
@Override
public List<T> list() {
return collQuery.list(path);
}
@Nullable
@Override
public T singleResult() {
return collQuery.singleResult(path);
}
@Nullable
@Override
public T uniqueResult() {
return collQuery.uniqueResult(path);
}
@Override
public SearchResults<T> listResults() {
return collQuery.listResults(path);
}
@Override
public long count() {
return collQuery.count();
}
@Override
public Query<T> limit(final long limit) {
collQuery.limit(limit);
return this;
}
@Override
public Query<T> offset(final long offset) {
collQuery.offset(offset);
return this;
}
@Override
public Query<T> restrict(final QueryModifiers modifiers) {
collQuery.restrict(modifiers);
return this;
}
@Override
public Query<T> orderBy(final OrderSpecifier<?>... o) {
collQuery.orderBy(o);
return this;
}
@Override
public <U> Query<T> set(final ParamExpression<U> param, final U value) {
collQuery.set(param, value);
return this;
}
@Override
public Query<T> distinct() {
collQuery.distinct();
return this;
}
@Override
public Query<T> where(final Predicate... o) {
collQuery.where(o);
return this;
}
}
有关信息,它已在querydsl 3.6.3中修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.