我正在为一个使用 JOOQ 库查询 SQL 的项目编写 UT,我需要模拟 SQL 响应。
我试过关注 this manual ,但是,我们将 JOOQ 与纯 SQL 一起使用,这意味着我们没有任何预定义的字段或表类。
我的问题是 - 如何在不提供 Field 作为参数的情况下定义 Result 对象?还是定义模拟字段参数?
Result<Record2<Integer, String>> result = create.newResult(AUTHOR.ID, AUTHOR.LAST_NAME); // replace the AUTHOR.ID and AUTHOR.LAST_NAME with some string for example
result.add(create.newRecord(AUTHOR.ID, AUTHOR.LAST_NAME));
result.get(0).setValue(AUTHOR.ID, 1);
result.get(0).setValue(AUTHOR.LAST_NAME, "Orwell");
mock[0] = new MockResult(1, result);
我注意到我可以定义一个新字段,但是 Field 实现的接口(interface)非常广泛,所以我正在寻找更简单的东西。
谢谢!
最佳答案
每当您在没有代码生成器的情况下使用 jOOQ 时,您将希望使用纯 SQL API 动态构建 Table
和 Field
引用。引用你的例子:
// Dynamic field creation
Field<Integer> id = field(name("AUTHOR", "ID"), SQLDataType.INTEGER);
Field<String> lastName = field(name("AUTHOR", "LAST_NAME"), SQLDataType.VARCHAR);
// Same as before
Result<Record2<Integer, String>> result = create.newResult(id, lastName);
result.add(create.newRecord(id, lastName).values(1, "Orwell"));
mock[0] = new MockResult(1, result);
有关详细信息,请参阅手册的以下部分:
关于java - 带有模拟字段的 JOOQ 模拟结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48282597/