StringUtils { public static String generateRandom(int length) { String generatedStr = null; if (length < 0) { return generatedStr; } if (length == 0) { return ""; } char[] generatedChar = new char[length]; int charsLength = chars.length; Random random = new SecureRandom(); for(int i = 0; i < length; ++i) { int index = random.nextInt(charsLength); generatedChar[i] = (char) chars[index]; } generatedStr = String.valueOf(generatedChar); return generatedStr; }  static String generateRandom(int length); static boolean isEmpty(String str);  }
@Test public void utilShouldReturnNullWhenLengthIsInvalid() { int length = -1; String str = StringUtils.generateRandom(length); assertNull(str); }
@Test public void utilShouldReturnEmptyStringWhenLengthIsZero() { int length = 0; String str = StringUtils.generateRandom(length); assertThat(str, is("")); }
@Test public void utilShouldReturnStringWhenLengthIsValid() { int length = 30; String str = StringUtils.generateRandom(length); assertNotNull(str); }
@Test public void utilShouldReturnCorrectLengthString() { int one = 1; int length = 3; int otherLength = 10; String oneStr = StringUtils.generateRandom(one); String str = StringUtils.generateRandom(length); String otherStr = StringUtils.generateRandom(otherLength); assertThat(oneStr.length(), is(one)); assertThat(str.length(), is(length)); assertThat(otherStr.length(), is(otherLength)); }