@Test
public void shouldThrowAuthenticationExceptionIfAuthenticationFails() throws Exception {

    final SaslException saslException = new SaslException("Ooops");
    final String host = "localhost";
    final int port = 2384;
    final String urlString = "service:jmx:remote+http://" + host + ":" + port;
    final Map<String, Object> environment = of("name", "value");
    final JmxParameters jmxParameters = jmxParameters()
            .withHost(host)
            .withPort(port)
            .build();
    final JMXServiceURL serviceURL = new JMXServiceURL(urlString);
    when(jmxUrlFactory.createUrl(host, port)).thenReturn(serviceURL);
    when(environmentFactory.create(jmxParameters)).thenReturn(environment);
    when(connectorWrapper.connect(serviceURL, environment)).thenThrow(saslException);
    try {
        jmxConnectorFactory.createJmxConnector(jmxParameters);
        fail();
    } catch (final JmxAuthenticationException expected) {
        assertThat(expected.getCause(), is(saslException));
        assertThat(expected.getMessage(), is("Jmx authentication failed"));
    }
}