【问题标题】:How to mock aggregate function result with JOOQ?如何使用 JOOQ 模拟聚合函数结果?
【发布时间】:2018-10-20 13:11:21
【问题描述】:

我使用 JOOQ 在我的服务类中调用计数聚合函数。

SelectQuery<Record> query = this.dsl.selectQuery();
query.addSelect(DSL.count());
query.addFrom(SOME_TABLE);
final Integer total = query.fetchOne(0, Integer.class);

我需要在单元测试中模拟计数结果。 最好的方法是什么?

jooq documentation 之后,我必须创建具有相关字段计数的结果记录。 类似的东西:

Result<Record1<Integer>> result = create.newResult(...);

但是在为聚合函数创建模拟记录的情况下,我必须使用什么作为 create.newResult() 方法参数?

【问题讨论】:

    标签: java unit-testing mocking jooq


    【解决方案1】:

    您的查询应该返回一行一列,因此创建该结果:

    Field<Integer> c = DSL.count();
    Result<Record1<Integer>> result = create.newResult(c);
    result.add(create.newRecord(c).values(42));
    

    您链接的文档显示了一个非常相似的示例:

    ...
    // You decide, whether any given statement returns results, and how many
    else if (sql.toUpperCase().startsWith("SELECT")) {
    
        // Always return one record
        Result<Record2<Integer, String>> result = create.newResult(AUTHOR.ID,AUTHOR.LAST_NAME);
        result.add(create
            .newRecord(AUTHOR.ID, AUTHOR.LAST_NAME)
            .values(1, "Orwell"));
        mock[0] = new MockResult(1, result);
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 2011-02-24
      • 2017-12-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多