ClassUtil { public static <T> T newInstance(String pClassName, Object ... pArguments) { Class<T> clazz = classForName(pClassName); if (clazz == null) { throw new IllegalArgumentException("Cannot find " + pClassName); } return newInstance(clazz, pArguments); } private  ClassUtil(); static Class<T> classForName(String pClassName, ClassLoader ... pClassLoaders); static Class<T> classForName(String pClassName,boolean pInitialize,ClassLoader ... pClassLoaders); static InputStream getResourceAsStream(String pPath); static boolean checkForClass(String pClassName); static T newInstance(String pClassName, Object ... pArguments); static T newInstance(Class<T> pClass, Object ... pArguments); static Object applyMethod(Object pObject, String pMethod, Object ... pArgs); static Set<String> getResources(String pResource);  }
@Test public void testNewInstance() { ClassUtilTest test = ClassUtil.newInstance(getClass().getCanonicalName()); assertEquals(test.getClass(),getClass()); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*find.*") public void testNewInstanceFail1() { ClassUtil.newInstance("blubber.bla"); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*NoSuchMethodException.*") public void testNewInstanceFail2() { ClassUtil.newInstance("java.lang.String",Boolean.TRUE); }
@Test public void testNewInstanceWithConstructor() { ClassUtilTest test = ClassUtil.newInstance(getClass().getCanonicalName(),"eins",2); assertEquals(test.getClass(),getClass()); assertEquals(test.stringProp,"eins"); assertEquals(test.intProp,2); }
@Test(expectedExceptions = IllegalArgumentException.class,expectedExceptionsMessageRegExp = ".*Blub.*") public void testNewInstanceNotFound() { ClassUtil.newInstance(getClass().getCanonicalName() + "$Blub"); }