Stephen Denne
Stephen Denne

Reputation: 37027

Randomize database row results

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?

Upvotes: 0

Views: 143

Answers (1)

Lauri Lehtinen
Lauri Lehtinen

Reputation: 10857

You could take a look at extending Hibernate's EmptyInterceptor, and specifically the onPrepareStatement method. If the sql query passed as the argument doesn't contain an order by clause, you could try adding order by random() to it.

Upvotes: 1

Related Questions