Wednesday, March 4, 2009

Oracle's dbms_random is really handy for tests.

I often want to throw random data at some of my unit tests. All I need to do is yank some data out of the database, randomize it, and set it as the driver for a test. Collections.shuffle() is a good way to do this when you can afford to pull a large number of rows into memory, but what if you have a massive data set and you want to get 5 rows, at random, out of the database? I add dbms_random.string('A',80) as random_number to my select statement, slap on an order by random_number at the bottom, and then add Query.setMaxResults(5) to my JPA query.

No comments:

Post a Comment