Code example for Constructor

Methods: newInstance, setAccessible

0
  @Ignore // todo we need to figure out a better way to deal with this... 
  @Test // the shadow will still have its default constructor called; it would be duplicative to call __constructor__() too. 
  @Config(shadows = {ShadowForClassWithNoDefaultConstructor.class}) 
  public void forClassWithNoDefaultConstructor_generatedDefaultConstructorShouldNotCallShadow() throws Exception { 
    Constructor<ClassWithNoDefaultConstructor> ctor = ClassWithNoDefaultConstructor.class.getDeclaredConstructor();
    ctor.setAccessible(true);
    ClassWithNoDefaultConstructor instance = ctor.newInstance();
    assertThat(Robolectric.shadowOf_(instance)).isNotNull();
    assertThat(Robolectric.shadowOf_(instance)).isInstanceOf(ShadowForClassWithNoDefaultConstructor.class);
    assertTrue(ShadowForClassWithNoDefaultConstructor.shadowDefaultConstructorCalled);
    assertFalse(ShadowForClassWithNoDefaultConstructor.shadowDefaultConstructorImplementorCalled);
  } 
 
  @Implements(ClassWithNoDefaultConstructor.class) 
  public static class ShadowForClassWithNoDefaultConstructor { 
    public static boolean shadowDefaultConstructorCalled = false;
    public static boolean shadowDefaultConstructorImplementorCalled = false;
 
    public ShadowForClassWithNoDefaultConstructor() { 
      this.shadowDefaultConstructorCalled = true;
    }