コード例 #1
0
  @Test
  public void settingInaccessibleFieldBypassingProtection() throws Exception {
    WithInaccessibleField target = new WithInaccessibleField();

    setField(target.getClass().getDeclaredField("i"), target, 3, true);

    assertEquals(3, target.i);
  }
コード例 #2
0
  @Test
  public void settingFieldWithoutBypassingProtection() throws Exception {
    WithAccessibleField target = new WithAccessibleField();

    setField(target.getClass().getField("i"), target, 2, false);

    assertEquals(2, target.i);
  }
コード例 #3
0
  @Test
  public void settingInaccessibleFieldWithoutBypassingProtection() throws Exception {
    WithInaccessibleField target = new WithInaccessibleField();

    thrown.expect(ReflectionException.class);
    thrown.expectMessage(IllegalAccessException.class.getName());

    setField(target.getClass().getDeclaredField("i"), target, 4, false);
  }