PasswordGenerator { public String generate() { StringBuilder sb = new StringBuilder(); sb.append(firstCharacterAlphabet.charAt(rng.nextInt(firstCharacterAlphabet.length()))); for (int i = 1; i < length; i++) { sb.append(alphabet.charAt(rng.nextInt(alphabet.length()))); } return sb.toString(); } PasswordGenerator(int length, String firstCharacterAlphabet, String alphabet);  PasswordGenerator(int length); String generate();  }
@Test public void length() { PasswordGenerator generator = new PasswordGenerator(10, "a", "a"); assertThat(generator.generate(), is("aaaaaaaaaa")); }
@Test public void alphabet() { PasswordGenerator generator = new PasswordGenerator(10, "ab", "ab"); assertThat(generator.generate().matches("[ab]{10}"), is(true)); }
@Test public void firstLetterAlphabet() { PasswordGenerator generator = new PasswordGenerator(10, "a", "b"); String password = generator.generate(); assertThat(password.substring(0, 1), is("a")); assertThat(password.substring(1, 10), is("bbbbbbbbb")); }