### Question:
PasswordGenerator { public String generatePassword(int totalLength, int minUppercase, int minLowercase, int numSpecial, int numDigits) { int remainingLetters = totalLength - (minUppercase + minLowercase + numSpecial + numDigits); if (remainingLetters < 0) { throw new IllegalArgumentException("More requirements than minimum letters"); } String upperCaseLetters = RandomStringUtils.random(minUppercase, A, Z, true, true, null, random); String lowerCaseLetters = RandomStringUtils.random(minLowercase, LOWERCASE_A, LOWERCASE_Z, true, true, null, random); String specialChar = RandomStringUtils.random(numSpecial, 0, 0, false, false, SPECIAL_CHARS, random); String numbers = RandomStringUtils.randomNumeric(numDigits); String remaining = RandomStringUtils.randomAlphanumeric(remainingLetters); String combinedChars = upperCaseLetters.concat(lowerCaseLetters) .concat(numbers) .concat(specialChar) .concat(remaining); List<String> chars = Arrays.asList(combinedChars.split("")); Collections.shuffle(chars); return chars.stream().collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString(); } PasswordGenerator();   PasswordGenerator(Random random); String generatePassword(int totalLength, int minUppercase, int minLowercase, int numSpecial, int numDigits);  }### Answer:
@Test public void testGeneratePasswords() { int len = 15; String pass = GENERATOR.generatePassword(len, 2, 2, 2, 2); assertEquals(len, pass.length()); assertContainsAll(pass, "J", "L", ".", ":"); PasswordGenerator gen = new PasswordGenerator(new Random(SEED)); String pass2 = gen.generatePassword(len, 2, 2, 2, 2); assertEquals(len, pass2.length()); assertContainsAll(pass2, "J", "L", ".", ":"); assertNotEquals("Generated the same password twice", pass, pass2); }
@Test(expected = IllegalArgumentException.class) public void testFailIfMoreRequirementsThanLength() { GENERATOR.generatePassword(5, 2, 2, 1, 1); }
@Test public void testSucceedIfRequirementsEqualLength() { GENERATOR.generatePassword(6, 2, 2, 1, 1); }