### Question:
CredentialsService { public void validate(Credentials credentials) throws InvalidCredentialsException { verifyNotEmptyLoginAtDomainAndPassword(credentials); verifyCertificateIsValid(credentials); }  void validate(Credentials credentials);  }### Answer:
@Test public void testCredentialsWithNiceCertificate() throws Exception { InputStream certificateInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("pkcs_pwd_toto.p12"); service.validate(Credentials.builder() .loginAtDomain("user@domain") .password("password".toCharArray()) .pkcs12(ByteStreams.toByteArray(certificateInputStream)) .pkcs12Password("toto") .build()); }
@Test public void testCredentialsWithBadCertificate() throws Exception { thrown.expect(InvalidCredentialsException.class); InputStream certificateInputStream = IOUtils.toInputStream("I'm not a pkcs12 file"); service.validate(Credentials.builder() .loginAtDomain("user@domain") .password("password".toCharArray()) .pkcs12(ByteStreams.toByteArray(certificateInputStream)) .pkcs12Password("") .build()); }
@Test public void testCredentialsWithoutCertificate() throws InvalidCredentialsException { service.validate(Credentials.builder() .loginAtDomain("user@domain") .password("password".toCharArray()) .build()); }
@Test public void testCredentialsWithEmptyLogin() throws Exception { thrown.expect(InvalidCredentialsException.class); Credentials credentials = createMock(Credentials.class); expect(credentials.getLoginAtDomain()).andReturn(""); replay(credentials); try { service.validate(credentials); } catch (Exception e) { verify(credentials); throw e; } }
@Test public void testCredentialsWithEmptyPassword() throws Exception { thrown.expect(InvalidCredentialsException.class); Credentials credentials = createMock(Credentials.class); expect(credentials.getLoginAtDomain()).andReturn("login@domain"); expect(credentials.getPassword()).andReturn("".toCharArray()); replay(credentials); try { service.validate(credentials); } catch (Exception e) { verify(credentials); throw e; } }