### Question:
SMTPServerConfiguration { @Bean public JavaMailSender getJavaMailSender() { JavaMailSenderImpl mailSender = createMailSender(); mailSender.setHost(hostname); mailSender.setPort(hostPort); if (isNotEmpty(username)) { mailSender.setUsername(username); } if (isNotEmpty(password)) { mailSender.setPassword(password); } Properties props = mailSender.getJavaMailProperties(); try { Map<String, String> map = configConverter.convertToMap(smtpConfigString); for (String key : map.keySet()) { props.put(key, map.get(key)); } } catch (Exception e) { LOG.error("Was not able to apply given smtp configuration"); } return mailSender; }  @Bean JavaMailSender getJavaMailSender();  }### Answer:
@Test public void defaults() { configToTest.hostname="host1"; configToTest.getJavaMailSender(); verify(mockedMailSender,never()).setPassword(any()); verify(mockedMailSender,never()).setUsername(any()); verify(mockedMailSender).setHost("host1"); verify(mockedMailSender).setPort(SMTPServerConfiguration.DEFAULT_SMTP_SERVER_PORT); assertEquals(2, properties.size()); assertEquals("smtp", properties.getProperty("mail.transport.protocol")); assertEquals("false", properties.getProperty("mail.smtp.auth")); }
@Test public void all_configured_all_set_to_mailsender() { configToTest.hostname="host1"; configToTest.hostPort=1234; configToTest.username="usr1"; configToTest.password="pwd1"; configToTest.smtpConfigString="mail.smtp.auth=true,mail.smtp.timeout=4321,mail.transport.protocol=smtp"; configToTest.getJavaMailSender(); verify(mockedMailSender).setPassword("pwd1"); verify(mockedMailSender).setUsername("usr1"); verify(mockedMailSender).setPort(1234); verify(mockedMailSender).setHost("host1"); assertEquals(3, properties.size()); assertEquals("smtp", properties.getProperty("mail.transport.protocol")); assertEquals("true", properties.getProperty("mail.smtp.auth")); assertEquals("4321", properties.getProperty("mail.smtp.timeout")); }
@Test public void all_configured_except_credentails_all_set_to_mailsender_but_credentials_not_set() { configToTest.hostname="host1"; configToTest.hostPort=1234; configToTest.username=""; configToTest.password=""; configToTest.smtpConfigString="mail.smtp.auth=false,mail.smtp.timeout=4321"; configToTest.getJavaMailSender(); verify(mockedMailSender,never()).setPassword(any()); verify(mockedMailSender,never()).setUsername(any()); verify(mockedMailSender).setPort(1234); verify(mockedMailSender).setHost("host1"); assertEquals(2, properties.size()); assertEquals("false", properties.getProperty("mail.smtp.auth")); assertEquals("4321", properties.getProperty("mail.smtp.timeout")); }