Code example for Constructor

Methods: getModifiers, newInstance, setAccessible

0
 
  @Test 
  public void shouldDelegateToHandlerForConstructors() throws Exception { 
    Class<?> clazz = loadClass(AClassWithNoDefaultConstructor.class);
    Constructor<?> ctor = clazz.getDeclaredConstructor(String.class);
    assertTrue(Modifier.isPublic(ctor.getModifiers()));
    ctor.setAccessible(true);
    Object instance = ctor.newInstance("new one");
    transcript.assertEventsSoFar("methodInvoked: AClassWithNoDefaultConstructor.__constructor__(java.lang.String new one)");
 
    Field nameField = clazz.getDeclaredField("name");
    nameField.setAccessible(true);
    assertNull(nameField.get(instance));
  } 
 
  @Test 
  public void shouldDelegateClassLoadForUnacquiredClasses() throws Exception { 
    ClassLoader classLoader = createClassLoader(new MySetup(false, false));
    Class<?> exampleClass = classLoader.loadClass(AnExampleClass.class.getName());
    assertSame(getClass().getClassLoader(), exampleClass.getClassLoader());
  }