### Question:
ClassFactories { public static <T> T newInstance(String className, Class<T> type) { try { Object instance = classForName(className).newInstance(); return type.cast(instance); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(format("No such class '%s'", className)); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } private  ClassFactories(); static T newInstance(String className, Class<T> type);  }### Answer:
@Test public void instantiatesClassWithZeroArgumentConstructor() { MyInterface instance = ClassFactories.newInstance(MyClass.class.getName(), MyInterface.class); assertThat(instance, is(instanceOf(MyClass.class))); }
@Test public void throwsExceptionIfThereIsNoZeroArgumentConstructor() { Exception e = assertThrows(RuntimeException.class, () -> ClassFactories.newInstance(MyInvalidClass.class.getName(), MyInterface.class)); assertEquals("java.lang.InstantiationException: com.hotels.styx.proxy.ClassFactoriesTest$MyInvalidClass", e.getMessage()); }
@Test public void throwsExceptionIfClassDoesNotExtendType() { Exception e = assertThrows(ClassCastException.class, () -> ClassFactories.newInstance(MyClass.class.getName(), Runnable.class)); assertEquals("Cannot cast com.hotels.styx.proxy.ClassFactoriesTest$MyClass to java.lang.Runnable", e.getMessage()); }
@Test public void throwsExceptionIfClassDoesNotExist() { Exception e = assertThrows(IllegalArgumentException.class, () -> ClassFactories.newInstance(MyClass.class.getName() + "NonExistent", Runnable.class)); assertEquals("No such class 'com.hotels.styx.proxy.ClassFactoriesTest$MyClassNonExistent'", e.getMessage()); }