@Test(expected = ExecutionException.class) public void executionExceptionThrownIfClassCannotBeInstantiatedWithNoArgs() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("argsRequired")); TestClass target = new TestClass(); unit.createAndBind(target); }
@Test(expected = ExecutionException.class) public void executionExceptionThrownIfFieldReprivatised() throws Exception { Field f = TestClass.class.getDeclaredField("privateConstructor"); FieldBinder unit = new FieldBinder(f); TestClass target = new TestClass(); f.setAccessible(false); unit.createAndBind(target); }
@Test public void createAndBindCreatesANewInstanceAndBindsItToTheField() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("another")); TestClass target = new TestClass(); Object binding = unit.createAndBind(target); assertThat(binding, instanceOf(AnotherClass.class)); assertThat(target.another, is(sameInstance(binding))); }