@Test public void testInstantiateTestSubject_existingNoArg() { String actual = Injector.instantiateTestSubject(field("noArg")); assertEquals("", actual); }
static <T> T instantiateTestSubject(Field f) { T testSubject; @SuppressWarnings("unchecked") Class<T> type = (Class<T>) f.getType(); if(type.isMemberClass() && !Modifier.isStatic(type.getModifiers())) { throw new AssertionError("TestSubject is an inner class. You need to instantiate '" + f.getName() + "' manually"); } Constructor<T> defaultConstructor; try { defaultConstructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new AssertionError("TestSubject is null and has no default constructor. You need to instantiate '" + f.getName() + "' manually"); } defaultConstructor.setAccessible(true); try { testSubject = defaultConstructor.newInstance(); } catch (ReflectiveOperationException e) { throw new AssertionError("TestSubject is null and default constructor fails on invocation. You need to instantiate '" + f.getName() + "' manually", e); } return testSubject; }
Injector { static <T> T instantiateTestSubject(Field f) { T testSubject; @SuppressWarnings("unchecked") Class<T> type = (Class<T>) f.getType(); if(type.isMemberClass() && !Modifier.isStatic(type.getModifiers())) { throw new AssertionError("TestSubject is an inner class. You need to instantiate '" + f.getName() + "' manually"); } Constructor<T> defaultConstructor; try { defaultConstructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new AssertionError("TestSubject is null and has no default constructor. You need to instantiate '" + f.getName() + "' manually"); } defaultConstructor.setAccessible(true); try { testSubject = defaultConstructor.newInstance(); } catch (ReflectiveOperationException e) { throw new AssertionError("TestSubject is null and default constructor fails on invocation. You need to instantiate '" + f.getName() + "' manually", e); } return testSubject; } }
Injector { static <T> T instantiateTestSubject(Field f) { T testSubject; @SuppressWarnings("unchecked") Class<T> type = (Class<T>) f.getType(); if(type.isMemberClass() && !Modifier.isStatic(type.getModifiers())) { throw new AssertionError("TestSubject is an inner class. You need to instantiate '" + f.getName() + "' manually"); } Constructor<T> defaultConstructor; try { defaultConstructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new AssertionError("TestSubject is null and has no default constructor. You need to instantiate '" + f.getName() + "' manually"); } defaultConstructor.setAccessible(true); try { testSubject = defaultConstructor.newInstance(); } catch (ReflectiveOperationException e) { throw new AssertionError("TestSubject is null and default constructor fails on invocation. You need to instantiate '" + f.getName() + "' manually", e); } return testSubject; }  }
Injector { static <T> T instantiateTestSubject(Field f) { T testSubject; @SuppressWarnings("unchecked") Class<T> type = (Class<T>) f.getType(); if(type.isMemberClass() && !Modifier.isStatic(type.getModifiers())) { throw new AssertionError("TestSubject is an inner class. You need to instantiate '" + f.getName() + "' manually"); } Constructor<T> defaultConstructor; try { defaultConstructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new AssertionError("TestSubject is null and has no default constructor. You need to instantiate '" + f.getName() + "' manually"); } defaultConstructor.setAccessible(true); try { testSubject = defaultConstructor.newInstance(); } catch (ReflectiveOperationException e) { throw new AssertionError("TestSubject is null and default constructor fails on invocation. You need to instantiate '" + f.getName() + "' manually", e); } return testSubject; }  static void injectMocks(Object host); }
Injector { static <T> T instantiateTestSubject(Field f) { T testSubject; @SuppressWarnings("unchecked") Class<T> type = (Class<T>) f.getType(); if(type.isMemberClass() && !Modifier.isStatic(type.getModifiers())) { throw new AssertionError("TestSubject is an inner class. You need to instantiate '" + f.getName() + "' manually"); } Constructor<T> defaultConstructor; try { defaultConstructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new AssertionError("TestSubject is null and has no default constructor. You need to instantiate '" + f.getName() + "' manually"); } defaultConstructor.setAccessible(true); try { testSubject = defaultConstructor.newInstance(); } catch (ReflectiveOperationException e) { throw new AssertionError("TestSubject is null and default constructor fails on invocation. You need to instantiate '" + f.getName() + "' manually", e); } return testSubject; }  static void injectMocks(Object host);  }