@Test
@SuppressWarnings("unchecked")
public void testProtocolProviderCreation() throws Exception {

  Iterator iterator = mock(Iterator.class);
  when(iterator.hasNext()).thenReturn(true, true, true, true);
  when(iterator.next()).thenReturn(getClientProtocolProvider())
      .thenThrow(new ServiceConfigurationError("Test error"))
      .thenReturn(getClientProtocolProvider());
  Iterable frameworkLoader = mock(Iterable.class);
  when(frameworkLoader.iterator()).thenReturn(iterator);
  Cluster.frameworkLoader = frameworkLoader;
  Cluster testCluster = new Cluster(new Configuration());
  // Check that we get the acceptable client, even after
  // failure in instantiation.
  assertNotNull("ClientProtocol is expected", testCluster.getClient());
  // Check if we do not try to load the providers after a failure.
  verify(iterator, times(2)).next();
}