Skip to content

Accessing static inner classes

stephanenicolas edited this page Sep 26, 2013 · 3 revisions

BoundBox provides access to all static inner classes (formally nested classes) defined in a given class.


Let's say we have a class Outer like :

@SuppressWarnings("unused")
public class Outer {
    private static class Inner {
    }
}

Note that the static inner class is private. Thus, the class Outer.Inner can't even be used as type to declare a variable outside of Outer. Nevertheless, with BoundBox, you can write a test that accesses the static inner class of Outer. You can also create a BoundBoxOfInner :

@BoundBox( boundClass = Outer.class )
public class OuterTest {
 
    @Test
    public void test_access_inner_class() {
        //GIVEN
        Outer outer = new Outer();
        //WHEN
        Object inner = BoundBoxOfOuter.boundBox_new_Inner();
       //THEN
        assertTrue(Outer.class.getDeclaredClasses()[0].isAssignableFrom(inner.getClass()));
    }

    @Test
    public void access_bound_box_of_inner_class() {
        //GIVEN
        Outer outer = new Outer();
        //WHEN
        Object inner = BoundBoxOfOuter.boundBox_new_Inner();
        //THEN
        assertNotNull( new BoundBoxOfOuter.BoundBoxOfInner(inner) );
     }
}

For all static inner classes in a class Outer, BoundBoxOfOuter will contain :

  • a static accessor to each of the constructors of the inner class : boundBox_new_Inner(...) ;
  • a static inner class to create a BoundBox of the static inner classes : BoundBoxOfInner.

The BoundBoxOfInner can then be used to access all fields and methods of Inner.