RandomStringUtils { public static String generateRandomAlphaNumericString(int length) { return generateRandomAlphaNumericString(length, new Random(System.currentTimeMillis())); }  static String generateRandomAlphaNumericString(int length); static String generateRandomAlphaNumericString(int length, Random rand);  }
@Test public void testGenerateRandomAlphaNumericString() { int length = 10; String result = RandomStringUtils.generateRandomAlphaNumericString(length); assertEquals(length, result.length()); length = 0; result = RandomStringUtils.generateRandomAlphaNumericString(length); assertEquals(length, result.length()); }
@Test public void testGenerateRandomAlphaNumericString_withProvidedRandom() { int length = 10; StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < length; i++) { strBuilder.append("a"); } Random rand = mock(Random.class); when(rand.nextInt(anyInt())).thenReturn(0); String result = RandomStringUtils.generateRandomAlphaNumericString(length, rand); assertEquals(length, result.length()); assertEquals(strBuilder.toString(), result); }