ComponentFactory { public static <T> T instantiate(Class<T> clazz, CRestConfig crestConfig) throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException { try { return accessible(clazz.getDeclaredConstructor(CRestConfig.class)).newInstance(crestConfig); } catch (NoSuchMethodException e) { return accessible(clazz.getDeclaredConstructor()).newInstance(); } } private  ComponentFactory(); static T instantiate(Class<T> clazz, CRestConfig crestConfig);  }
@Test public void shouldInstantiateAccessibleWithCRestConfig() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { AccessiblePublicClassWithCRestConfig actual = ComponentFactory.instantiate(AccessiblePublicClassWithCRestConfig.class, mockCRestConfig); assertNotNull(actual); assertSame(mockCRestConfig, actual.crestConfig); }
@Test public void shouldInstantiateAccessibleWithoutCRestConfig() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { AccessiblePublicClassNoCRestConfig actual = ComponentFactory.instantiate(AccessiblePublicClassNoCRestConfig.class, mockCRestConfig); assertNotNull(actual); }
@Test(expected=NoSuchMethodException.class) public void shouldFailInstantiateClassWithUnknownConstructors() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { ComponentFactory.instantiate(AccessiblePublicClassWithUninstantiable.class, mockCRestConfig); }
@Test public void shouldInstantiateNotAccessibleWithCRestConfig() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { NotAccessiblePublicClassWithCRestConfig actual = ComponentFactory.instantiate(NotAccessiblePublicClassWithCRestConfig.class, mockCRestConfig); assertNotNull(actual); assertSame(mockCRestConfig, actual.crestConfig); }
@Test public void shouldInstantiateNotAccessibleWithoutCRestConfig() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { NotAccessiblePublicClassNoCRestConfig actual = ComponentFactory.instantiate(NotAccessiblePublicClassNoCRestConfig.class, mockCRestConfig); assertNotNull(actual); }
@Test(expected=NoSuchMethodException.class) public void shouldFailInstantiateNotAccessibleClassWithUnknownConstructors() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { ComponentFactory.instantiate(NotAccessiblePublicClassWithUninstantiable.class, mockCRestConfig); }