Testing a class which utilizes randomness

Nikola Novak <[email protected]> Fri, 4 Apr 2014 11:29:21 +0200
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <CA+AeE3YSWzMxE9n=RhrTxzCa5ruNEod_pdkYkVHtg8bvJN3CRQ@mail.gmail.com>
Hello!

I'm new to TDD and this group. For practice, I started a simple project and
now I want to create a list of random strings, but such that no two strings
are ever the same. In a very unlikely, yet possible case, when two randomly
generated strings are the same, the method that does this should discard
the duplicate and generate a new string before it returns.

What I want to test is exactly this behavior - the code which discards the
duplicate and forces the string to be regenerated. To test that, I need to
force the random string generator to return a duplicate at least once (but
finite number of times). To create the test which would generate strings
until there's at least one duplicate is unrealistic, because such a test
could take a very long time to complete.

How would I write such a test?

Kind regards,
Nikola