/** * Dog has two fields of type String. We can't known which one should be * retrieved. */ @Test(expected = NoSuchFieldError.class) public void testAmbiguousGet() { Dog dog = new Dog(); ReflectionHelper.getValueInFieldOfType(dog, String.class); }
/** * Simplest use case: retrieve a field stored in the class itself. */ @Test public void testGet() { Monkey monkey = new Monkey(); String name = ReflectionHelper.getValueInFieldOfType(monkey, String.class); assertEquals(monkey.name, name); }
/** * Tests whether the list (stored in a parent class) is found. */ @Test public void testGetInParent() { Monkey monkey = new Monkey(); List<?> friends = ReflectionHelper.getValueInFieldOfType(monkey, List.class); assertEquals(monkey.friends, friends); }
/** * Monkey has two fields of type boolean. In {@link #testAmbiguousGet()}, * this was a problem. However, in this case one of the booleans in stored * in the parent class, so it is clear which field should be retrieved. */ @Test public void testActuallyNotAmbiguousGet() { Monkey monkey = new Monkey(); boolean isClimbing = ReflectionHelper.getValueInFieldOfType(monkey, boolean.class); assertEquals(monkey.isClimbing, isClimbing); }