Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Sometimes when writing a test, you'll need to access data from a class that does not have a public API to access the data.  In ONOS, we try to not add interfaces only used by test code.  Instead, we have utility methods that use the Java reflection API to access this private data.  In this example, the test verifies the contents of the private intentsByLink  member are correct, using the TestUtils.getField() method.

Code Block
languagejava
titleAccessing Private Data in a Test
import org.onlab.junit.TestUtils;
 
    @Test
    public void testLeaderEvents() throws Exception {
        final ObjectiveTracker tracker = new ObjectiveTracker();

        final SetMultimap<LinkKey, IntentId> intentsByLink =
                TestUtils.getField(tracker, "intentsByLink");
        assertThat(intentsByLink.size(), is(0));
    }

...