使用querydsl模拟数据库查询 - 可选问题

时间:2015-03-13 16:02:00

标签: java junit mocking guava querydsl

我在为我的应用程序编写数据库查询测试时遇到了一些困难,它在mongo上使用了querydsl。我发现了一些只是对查询对象本身进行单元测试的人的例子,但是我想更进一步,测试查询的执行方式(如集成测试)但不必启动整个数据库进程。也就是说,使用java工具专门模拟数据库。

我找不到任何东西,DBUnit或DbSetup等工具需要与DB的实际连接。所以我开始编写自己的类,它几乎可以工作。我们的想法是使用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);
    }
}

...基本上,它似乎工作!除了我的代码大量使用guava的Optional,它似乎是querydsl的一个问题。如果MyObject取代String而不是String,则取一个&lt;字符串&gt; ,然后我收到错误:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

有关信息,已在querydsl 3.6.3

中修复