JmsConnectionHealthChecker { public Result check(ConnectionFactory connectionFactory) { try (Connection connection = connectionFactory.createConnection()) { if (connection == null) { return Result.unhealthy("No connection could be established"); } connection.start(); return Result.healthy(); } catch (JMSException e) { return Result.unhealthy(e); } }  Result check(ConnectionFactory connectionFactory);  }
@Test public void checkHealthy() throws Exception { Connection connectionMock = Mockito.mock(Connection.class); when(connectionFactory.createConnection()).thenReturn(connectionMock); assertThat(healthChecker.check(connectionFactory).isHealthy(), is(true)); }
@Test public void checkUnhealthyNoConnection() { assertThat(healthChecker.check(connectionFactory).isHealthy(), is(false)); }
@Test public void checkUnhealthyJmsException() throws Exception { when(connectionFactory.createConnection()).thenThrow(new JMSException("failed")); assertThat(healthChecker.check(connectionFactory).isHealthy(), is(false)); }
@Test public void checkUnhealthyCloseConnection() throws Exception { Connection connectionMock = Mockito.mock(Connection.class); doThrow(new JMSException("")).when(connectionMock).close(); when(connectionFactory.createConnection()).thenReturn(connectionMock); assertThat(healthChecker.check(connectionFactory).isHealthy(), is(false)); }
@Test public void checkUnhealthyExceptionOnStartConnection() throws Exception { Connection connectionMock = Mockito.mock(Connection.class); doThrow(new JMSException("")).when(connectionMock).start(); when(connectionFactory.createConnection()).thenReturn(connectionMock); assertThat(healthChecker.check(connectionFactory).isHealthy(), is(false)); }