Skip to content

Adding extra fields to a boundbox

stephanenicolas edited this page Sep 25, 2013 · 3 revisions

Extra fields offers a way to access fields of a given class, even if they are not defined in the class...

Think about Android for instance : when you compile your code, you use the Android jars that only contain stub methods (i.e. methods with the right signature of Android SDK, but with a body that send a "Stub !" exception). So, if you create a BoundBox of an Android class, it will give you access to all methods, but won't contain any of the fields of this class.

In that case, and for all other jars with the provided scope, you might want to add additional fields to a BoundBoxOfFoo class.

--

Let's say we have a class A like:

public class A {
	//this is a stub, the real class has some fields but there are not visible in your test environnement
}

and the real class:

public class A {
	private String foo = "bb";
}

With BoundBox extraFields, you can write a test that will let you access the field foo:

public class ATest {
 @BoundBox( boundClass = A.class,
	    extraFields = {
	        @BoundBoxField(fieldName = "foo", fieldClass = String.class)
	    })
 private BoundBoxOfA boundBoxOfA;
 
 @Before
 public void setUp() {
   boundBoxOfA = new BoundBoxOfA( new A() );
 }
 
 @Test
 public void testFieldRead() {
   //GIVEN
   //WHEN
   //THEN
   assertEquals( "bb", boundBoxOfA.boundBox_getFoo());
 }

 @Test
 public void testFieldWrite() {
   //GIVEN
   //WHEN
   boundBoxOfA.boundBox_setFoo("cc");
   //THEN
   assertEquals( "cc", boundBoxOfA.boundBox_getFoo());
 }
}

--

Please, note that you can specify more than one extra fields.