4444 *
4545 * @author Johannes Edmeier
4646 * @author Stephane Nicoll
47+ * @author Scott Frederick
4748 */
4849class MailHealthIndicatorTests {
4950
@@ -61,6 +62,52 @@ void setup() {
6162 this .indicator = new MailHealthIndicator (this .mailSender );
6263 }
6364
65+ @ Test
66+ void smtpOnDefaultHostAndPortIsUp () {
67+ given (this .mailSender .getHost ()).willReturn (null );
68+ given (this .mailSender .getPort ()).willReturn (-1 );
69+ given (this .mailSender .getProtocol ()).willReturn ("success" );
70+ Health health = this .indicator .health ();
71+ assertThat (health .getStatus ()).isEqualTo (Status .UP );
72+ assertThat (health .getDetails ()).doesNotContainKey ("location" );
73+ }
74+
75+ @ Test
76+ void smtpOnDefaultHostAndPortIsDown () throws MessagingException {
77+ given (this .mailSender .getHost ()).willReturn (null );
78+ given (this .mailSender .getPort ()).willReturn (-1 );
79+ willThrow (new MessagingException ("A test exception" )).given (this .mailSender ).testConnection ();
80+ Health health = this .indicator .health ();
81+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
82+ assertThat (health .getDetails ()).doesNotContainKey ("location" );
83+ Object errorMessage = health .getDetails ().get ("error" );
84+ assertThat (errorMessage ).isNotNull ();
85+ assertThat (errorMessage .toString ().contains ("A test exception" )).isTrue ();
86+ }
87+
88+ @ Test
89+ void smtpOnDefaultHostAndCustomPortIsUp () {
90+ given (this .mailSender .getHost ()).willReturn (null );
91+ given (this .mailSender .getPort ()).willReturn (1234 );
92+ given (this .mailSender .getProtocol ()).willReturn ("success" );
93+ Health health = this .indicator .health ();
94+ assertThat (health .getStatus ()).isEqualTo (Status .UP );
95+ assertThat (health .getDetails ().get ("location" )).isEqualTo (":1234" );
96+ }
97+
98+ @ Test
99+ void smtpOnDefaultHostAndCustomPortIsDown () throws MessagingException {
100+ given (this .mailSender .getHost ()).willReturn (null );
101+ given (this .mailSender .getPort ()).willReturn (1234 );
102+ willThrow (new MessagingException ("A test exception" )).given (this .mailSender ).testConnection ();
103+ Health health = this .indicator .health ();
104+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
105+ assertThat (health .getDetails ().get ("location" )).isEqualTo (":1234" );
106+ Object errorMessage = health .getDetails ().get ("error" );
107+ assertThat (errorMessage ).isNotNull ();
108+ assertThat (errorMessage .toString ().contains ("A test exception" )).isTrue ();
109+ }
110+
64111 @ Test
65112 void smtpOnDefaultPortIsUp () {
66113 given (this .mailSender .getPort ()).willReturn (-1 );
0 commit comments