@Test
  public void settingInaccessibleFieldBypassingProtection() throws Exception {
    WithInaccessibleField target = new WithInaccessibleField();

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

    assertEquals(3, target.i);
  }
  @Test
  public void settingFieldWithoutBypassingProtection() throws Exception {
    WithAccessibleField target = new WithAccessibleField();

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

    assertEquals(2, target.i);
  }
  @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);
  }
Example #4
0
  @Override
  public T generate(SourceOfRandomness random, GenerationStatus status) {
    Class<T> type = types().get(0);
    Object generated = instantiate(type);

    for (Field each : fields) {
      ParameterContext parameter = parameterContext(each);
      setField(each, generated, generatorFor(parameter).generate(random, status), true);
    }

    return type.cast(generated);
  }