コード例 #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 settingInaccessibleFieldWithoutBypassingProtection() throws Exception {
    WithInaccessibleField target = new WithInaccessibleField();

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

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