Time for action – avoiding SWTBot runtime errors

As more test methods are added, the runtime may start throwing spurious errors. This is because the order of the tests may cause changes, and the ones that ran previously may modify the state of the workbench. This can be mitigated by moving the common setup and tear-down routines to a single place:

  1. Create a static method beforeClass().
  2. Add the annotation @BeforeClass from the org.junit package.
  3. Move references to create a SWTWorkbenchBot to the static method, and save the value in a static field.
  4. The code looks like this:
    private static SWTWorkbenchBot bot;
    @BeforeClass
    public static void beforeClass() {
      bot = new SWTWorkbenchBot();
      try {
        bot.viewByTitle("Welcome").close();
      } catch (WidgetNotFoundException e) {
        // ignore
      }
    }
  5. Run the tests and ensure that they pass appropriately.

What just happened?

The JUnit annotation @BeforeClass allows a single static method to be executed prior to any of the tests running in the class. This is used to create an instance of SWTWorkbenchBot, which is then used by all other tests in the class. This is also an opportune location to close the Welcome view, if it is shown, so that all other tests can assume that the window has been cleaned up.

Do not call bot.resetWorkbench(), otherwise subsequent tests will fail in the test cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset