[ovirt-devel] JUnit rule for testing UiCommon code

Vojtech Szocs vszocs at redhat.com
Mon Aug 4 15:01:10 UTC 2014


Hi,

I wrote a small patch to aid UI developers in testing UiCommon code:

  http://gerrit.ovirt.org/#/c/31021/

This patch adds a JUnit rule used to set up the necessary UiCommon
infrastructure (represented by mock objects) as expected by existing
code (hidden expectations represented by static calls).

You can also access these (infrastructure) mock objects to stub their
behavior, if necessary.

Example - UiCommon infrastructure setup per test class:

  public class MyTest {

    @ClassRule
    public static UiCommonSetup setup = new UiCommonSetup();

    // This is optional, but often necessary
    @BeforeClass
    public static void stubUiCommonInfra() {
      AsyncDataProvider adp = setup.getMocks().asyncDataProvider();
      when(adp.isWindowsOsType(anyInt())).thenReturn(true);
    }

    // Actual test code to exercise model instance(s)

  }

Example - UiCommon infrastructure setup per test method:

  public class MyTest {

    @Rule
    public UiCommonSetup setup = new UiCommonSetup();

    // This is optional, but often necessary
    @Before
    public void stubUiCommonInfra() {
      AsyncDataProvider adp = setup.getMocks().asyncDataProvider();
      when(adp.isWindowsOsType(anyInt())).thenReturn(true);
    }

    // Actual test code to exercise model instance(s)

  }

When creating specific model instances (test subjects), stubbing
methods to avoid hidden static calls shouldn't be necessary anymore.

Regards,
Vojtech



More information about the Devel mailing list