ServiceBootstrap { public static <S> S loadFirst(Class<S> clazz) { Iterator<S> iterator = loadAll(clazz); if (!iterator.hasNext()) { throw new IllegalStateException(String.format( "No implementation defined in /META-INF/services/%s, please check whether the file exists and has the right implementation class!", clazz.getName())); } return iterator.next(); }  static S loadFirst(Class<S> clazz); static Iterator<S> loadAll(Class<S> clazz); static List<S> loadAllOrdered(Class<S> clazz); static S loadPrimary(Class<S> clazz);  }
@Test public void loadFirstSuccessfully() throws Exception { Interface1 service = ServiceBootstrap.loadFirst(Interface1.class); assertTrue(service instanceof Interface1Impl); }
@Test(expected = IllegalStateException.class) public void loadFirstWithNoServiceFileDefined() throws Exception { ServiceBootstrap.loadFirst(Interface2.class); }
@Test(expected = IllegalStateException.class) public void loadFirstWithServiceFileButNoServiceImpl() throws Exception { ServiceBootstrap.loadFirst(Interface3.class); }
@Test(expected = ServiceConfigurationError.class) public void loadFirstWithWrongServiceImpl() throws Exception { ServiceBootstrap.loadFirst(Interface4.class); }
@Test(expected = ServiceConfigurationError.class) public void loadFirstWithServiceImplNotExists() throws Exception { ServiceBootstrap.loadFirst(Interface5.class); }