LibraryToggle FramesPrintFeedback

JUnit 4 is the latest version of the JUnit Java testing suite. What distinguishes JUnit 4 from earlier versions is that JUnit 4 defines a test by applying Java annotations (in contrast to earlier versions of JUnit, which used inherited classes and naming conventions).

The simplest JUnit tests require just two steps:

  1. Specify which methods are the test methods by annotating them with the @org.junit.Test annotation.

  2. At any point in the test method, define an assertion by calling assertTrue() with a boolean argument (you also need to include a static import of org.junit.Assert.assertTrue() in the file). The test succeeds, if the specified assertions all evaluate to true.

To integrate JUnit 4 with Pax-Exam, perform the following steps:

  1. Customize JUnit to run the test in the Pax-Exam test runner class—JUnit allows you to delegate control over a test run to a custom runner class (by defining a runner class that inherits from org.junit.runner.Runner). In order to integrate JUnit with Pax-Exam, add a @RunWith annotation to the test class as follows:

    import org.junit.runner.RunWith;
    import org.ops4j.pax.exam.junit.JUnit4TestRunner;
    ...
    @RunWith(JUnit4TestRunner.class) 
    public class MyTest { 
        ...
    }
  2. Add a Pax-Exam configuration method to the test class—in order to run a test in an OSGi framework, you need to initialize the OSGi container properly and install any prerequisite bundles. These essential steps are performed by returning the appropriate options from the Pax-Exam configuration method. This method is identified by the @Configuration annotation as follows:

    import org.ops4j.pax.exam.junit.Configuration;
    ...
    @Configuration
    public static Option[] configuration() throws Exception {
        ...
    }
  3. Use the Pax-Exam fluent API to configure the OSGi framework—there are a fairly large number of settings and options that you can return from the Pax-Exam configuration method. In order to define these options efficiently, Pax-Exam provides a fluent API, which is defined mainly by the following classes:

Example 13.1 shows the Maven dependencies you need in order to run the Pax-Exam testing framework. You must specify dependencies on JUnit 4, Pax-Exam, and Apache Karaf tooling.


This example uses custom properties to specify the versions of the various Maven artifacts.

Comments powered by Disqus
loading table of contents...