Time for action – writing a plug-in test

Although Java projects and Java plug-in projects both use Java and JUnit to execute, plug-ins typically need to have access to them (provided by the runtime platform), which is only available if running in an OSGi or Eclipse environment.

  1. Create a new plug-in project called com.packtpub.e4.junit.plugin.
  2. Create a new JUnit test called PlatformTest in the com.packtpub.e4.junit.plugin package.
  3. Create a method called testPlatform(), which asserts that the Platform is running:
    @Test
    public void test() {
      assertTrue(Platform.isRunning());
    }
  4. Click on the quick-fix to add org.junit to the required bundles.
    • Alternatively, open up the project's manifest by right-clicking on it and going to Plug-in Tools | Open Manifest.
    • Go to the Dependencies tab and click on Add, and select org.junit from the dialog.
    • Ensure that org.eclipse.core.runtime is also added as a dependency.
  5. Run the test by right-clicking on the project and going to Run As | JUnit Test. You will see the error message fail (with an assertion error).
  6. Run the test as a plug-in, by right-clicking on the project and going to Run As | JUnit Plug-in Test. You will see the test pass.

What just happened?

Although the test code is exactly the same, the way in which the tests are run is slightly different. In the first instance, it uses the standard JUnit test runner, which executes the code in a standalone JVM. Since this doesn't have the full Eclipse runtime inside, the test fails.

The plug-in test is launched in a different way: a new Eclipse instance is created, the plug-in is exported and installed into the runtime, the various OSGi services that are needed to power Eclipse are brought up, and then the test runner executes the plugin in place.

As a result, running a plug-in test can add latency to the test process, because the platform has to be booted first. Sometimes, quick-tests are run as standalone Java tests, while integration tests run in the context of a full plug-in environment.

Code sections that depend on OSGi and Platform services need to be run as plug-in tests.

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

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