Time for action – avoiding SWTBot runtime errors

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

  1. Create a static method beforeClass.
  2. Add the annotation @BeforeClass from the org.junit package.
  3. Move references to creating a SWTWorkbenchBot to the static method, and save the value in a static field.
  4. The code looks like:
    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 the 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 as appropriate.

Do not call the bot.resetWorkbench() as 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
18.117.142.128