gpt4 book ai didi

java - 如何用JOOQ模拟聚合函数结果?

转载 作者:行者123 更新时间:2023-11-30 02:10:28 25 4
gpt4 key购买 nike

我使用 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()为聚合函数创建模拟记录时的方法参数?

最佳答案

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

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

关于java - 如何用JOOQ模拟聚合函数结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50269936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com