Skip to content

Accessing inherited & hidden fields

stephanenicolas edited this page Sep 12, 2013 · 5 revisions

BoundBox provides access to all fields defined in a given class, and all fields of its super class.


Let's say we have 2 classes A & B like :

@SuppressWarnings("unused")
public class A {
 private String foo = "a";
 private int bar = 2;
}

@SuppressWarnings("unused")
public class B extends A {
 private String foo = "b";
}

With BoundBox, you can write a test that accesses fields of A and B :

public class BTest {
 @BoundBox( boundClass = B.class )
 private BoundBoxOfB boundBoxOfB;
 
 @Before
 public void setUp() {
   boundBoxOfB = new BoundBoxOfB( new B() );
 }
 
 @Test
 public void testFieldInB() {
   //GIVEN
   //WHEN
   //THEN
   assertEquals( "b", boundBoxOfB.boundBox_getFoo());
 }

 @Test
 public void testInheritedFieldInB() {
   //GIVEN
   //WHEN
   //THEN
   assertEquals( 2, boundBoxOfB.boundBox_getBar());
 }

 @Test
 public void testHiddenFieldInA() {
   //GIVEN
   //WHEN
   //THEN
   assertEquals( "a", boundBoxOfB.boundBox_super_A_getFoo());
 }

}

For all fields defined in a class B, its BoundBox will contain setters and getters, prefixed with boundBox_. For all fields defined in super classes of B :

  • if field is not hidden : it will be "inherited" and presented as a field of B directly (see above).
  • if field is hidden : BoundBox of B will contain setters and getters for those fields, prefixed with boundBox_super + <superClassName> + _.

This allows to avoid collisions with the setters and getters defined in B itself.

Clone this wiki locally