随机化数据库行结果
我发现一些单元测试假设数据库查询的结果按特定顺序排列,但正在运行的查询不包含 order by
子句。
我想找到更多这样的单元测试,以便我可以检查测试是否在其假设中存在错误,或者代码是否在缺乏指定顺序方面存在错误。
我正在使用 java、junit、spring、hibernate、dbunit、jdbc 和 postgresql。
我的一个想法是在某处拦截测试查询,如果查询不包含 order by 子句,则捕获所有结果,并以随机顺序返回它们。
哪里是最容易拦截和检查查询的地方?
还有其他简单的方法来识别此类测试吗?
I've been finding some unit tests that assume that the results of database queries are in a particular order, yet the query being run doesn't include an order by
clause.
I would like to find more of these unit tests, so that I can examine whether the test is at fault in its assumptions, or the code is at fault in its lack of specifying an order.
I'm using java, junit, spring, hibernate, dbunit, jdbc, and postgresql.
One idea I had was to intercept the test queries somewhere, and if a query does not include an order by clause, then capture all the results, and return them in a random order.
Where would be the easiest place to intercept and check the query?
Are there other simple ways of identifying such tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以看看扩展 Hibernate 的
EmptyInterceptor
,特别是onPrepareStatement
方法。如果作为参数传递的 sql 查询不包含order by
子句,您可以尝试向其中添加order by random()
。You could take a look at extending Hibernate's
EmptyInterceptor
, and specifically theonPrepareStatement
method. If the sql query passed as the argument doesn't contain anorder by
clause, you could try addingorder by random()
to it.