我的應用程序的數據庫查詢編寫測試時遇到了一些困難,該應用程序在mongo上使用querydsl。我發現了幾個人只是單元測試查詢對象本身的例子,但我想更進一步,並測試查詢如何執行(如集成測試),但無需啓動整個數據庫進程。也就是說,只用java工具嘲笑數據庫。使用querydsl嘲笑db查詢 - 問題與可選
我找不到任何東西,DBUnit或DbSetup等工具需要實際連接到數據庫。所以我開始寫我自己的課程,它幾乎可行。這個想法是使用com.mysema.query.collections.CollQuery和mockito來模擬一個數據庫,該數據庫可以接收我的應用程序的查詢,並使用包裝器「CollQuery to Query」。基本上,它是這樣的:
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);
}
}
...基本上,它似乎工作!除了我的代碼大量使用番石榴的Optional,並且它似乎是querydsl的一個問題。如果,而不是字符串,爲MyObject有一個可選的<字符串>,然後我得到的錯誤:
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)
所以,它看起來像mysema試圖實例化com.google.common.base.Present,但無法這樣做。奇怪的部分是,相同的查詢完全符合我的生產代碼,所以也許這個問題對於我用於模擬的「CollQuery」類非常具體。
有關如何解決這個問題的任何想法?或者,如果有人可以看到不同的方式來模擬數據庫,就像我正在嘗試做的那樣,我將不勝感激任何幫助!
感謝
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;
}
}
生成的代碼是小於有用你在這裏測試的代碼。我的直覺是,你應該簡單地通過對象期望的東西,除非你不希望在該級別使用'Optional'。 – Makoto 2015-03-13 16:04:02
我的數據模型充滿了可選的,它是由一種「Model to Mongo」適配器在較低級別管理的 – Joel 2015-03-13 16:07:08
有關信息,我在querydsl處打開了一個問題https://github.com/querydsl/querydsl/issues/1262 – Joel 2015-03-16 19:51:57