@Test
public void shouldThrowExceptionIfConnectingToTheMBeanFails() throws Exception {

    final IOException ioException = new IOException("Ooops");
    final String contextName = "my-context";
    final Class<SystemCommanderMBean> mBeanInterface = SystemCommanderMBean.class;
    final JMXConnector jmxConnector =  mock(JMXConnector.class);
    final ObjectName objectName = mock(ObjectName.class);
    when(commandMBeanNameProvider.create(contextName)).thenReturn(objectName);
    when(jmxConnector.getMBeanServerConnection()).thenThrow(ioException);
    try {
        mBeanConnector.connect(contextName, mBeanInterface, jmxConnector);
        fail();
    } catch (final MBeanClientConnectionException expected) {
        assertThat(expected.getCause(), is(ioException));
        assertThat(expected.getMessage(), is("Failed to get remote connection to MBean 'SystemCommanderMBean'"));
    }
}