我正在为将数据导入数据库的脚本编写一些测试。脚本的单元测试看起来或多或少像
doAnImport(fileName, databaseToImportInto)
Result<Record2<String, Integer>> results
= databaseToImportInto.select(aStringField, anIntegerField)
.from(oneTable)
.join(anotherTable).on(someField.eq(someOtherField)
.fetch();
我现在想做的是 Junit 断言,例如
assertEquals(10, results.getSize());
assertTrue(results.contains(new Record2<String, Integer>(expectedString1, expectedInteger1);
...
assertTrue(results.contains(new Record2<String, Integer>(expectedString10, expectedInteger10);
因为我不想假设结果中有任何特定的顺序。我的问题是 Record2 是一个接口(interface),而 RecordImpl 不公开可见。
我能看到的其他选项(将测试查询制作成 View 并为其生成类,以便有一个 XXXRecord 实现)对我来说并不是真正可用的——这些查询越来越多,我不想要将我的数据库模式与我的测试用例的特性联系起来。
我想我可以创建我自己的 RecordImpl 子类,但这似乎需要很多工作,所以我猜想考虑到库其余部分的优雅(如果你还没有尝试过,你真的应该) ,这不是“正确的事情”。
如有任何帮助,我们将不胜感激。
最佳答案
我认为实现这一目标的最简单方法是:
assertEquals(new HashSet<>(result1), new HashSet<>(result2));
这将比较两个 Result
对象,忽略记录顺序。这就是您创建临时记录的方式:
Record record = DSL.using(...).newRecord(result1.fields());
但确实可能存在改进 API 的空间。
关于java - 如何在 JOOQ 中手动创建 RecordN 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662424/