What's a good way to generate a random number for a valid credit card?
Not so long ago I've authored a library called MockNeat. One of the features is to allow the developer to generate different valid credit card numbers.
Check this method: creditCards().
A short example to write 1000 Credit Cards AMEX and Mastercard in a file for later use:
MockNeat m = MockNeat.threadLocal();
final Path path = Paths.get("cc.txt");
// Write in a file 1000 credit cards AMEX and Mastercard:
m.creditCards()
.types(MASTERCARD, AMERICAN_EXPRESS)
.list(1000)
.consume(list -> {
try { Files.write(path, list, CREATE, WRITE); }
catch (IOException e) { e.printStackTrace(); }
});
This functionality will (hopefully) make my test cases a bit more reliable.
I'm not convinced. In my experience, it is not a good idea to use random data in unit tests, because you never know if you've covered all of the important cases ... and bugs.
I'd recommend creating your test credit card numbers by hand, and taking care that they cover all of the cases that need to be tested.