TestNG Driver

Overview

The TestNG driver is functionally equivalent to the JUnit driver (but not tested as thoroughly). It supports all operation modes (OSGi, Java EE, CDI, Web). JUnit continues to be the preferred driver for Pax Exam, due to its more flexible customization hooks and cleaner API.

Here is a skeleton of a TestNG test for Pax Exam 3:

@Listeners(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class SampleTest {

    @Inject
    private BundleContext bc;

    @Configuration
    public Option[] config1() {
       // ...
    }

    @Configuration
    public Option[] config2() {
       // ...
    }

    @BeforeMethod
    public void setUp() {
        // ...
    }

    @AfterMethod
    public void tearDown() {
        // ...
    }

    @Test
    public void test1() {
       // ...
    }

    @Test
    public void test2() {
       // ...
    }

    @Test
    public void test3() {
       // ...
    }
}

Points to note

  • @BeforeMethod and @AfterMethod are the only configuration methods supported by Pax Exam. They will run in the container (which is what you want) but not in the driver.
  • @BeforeClass, @BeforeTest, @BeforeSuite and the corresponding @After... methods are not supported. If you use them, they will run twice, once by the driver and once in the container, which is probably not what you expect.
  • The @ExamReactorStrategy(...) annotation lets you specify a reactor strategy. It is optional and defaults to @PerMethod for OSGi mode and to @PerSuite for Java EE and CDI modes.
  • In OSGi mode, you need at least one no-args method with return type Option[] and annotated by @Configuration. The method name does not matter. If there is more than one configuration method, each test method will be run for each of the given configurations. In other modes, this method is optional and can be used to deploy additional modules.
  • Test methods are identified by the usual @org.testng.annotations.Test annotation.
  • In OSGi mode, you currently need to provision TestNG explicitly:
mavenBundle("org.testng", "testng", "6.3.1"),

In Java EE and CDI modes, TestNG is picked up from the driver class path.

Compatibility Note

  • The TestNG driver no longer publishes its listener to the java.util.ServiceLoader via META-INF/services as in Pax Exam 2.x. This is to prevent Pax Exam from interfering with plain old TestNG tests in the same project. You can either add a @Listeners annotation to all your test classes, or create a file META-INF/services/org.testng.ITestNGListener in your own project with the following contents:
org.ops4j.pax.exam.testng.listener.PaxExam
  • The old listener ExamTestNGListener from Pax Exam 2.x is still available but deprecated. It is simply an alias for PaxExam.
  • Pax Exam test annotations @Configuration, @ExamFactory, @ExamReactorStrategy now come from package org.ops4j.pax.exam so they can be shared with the JUnit driver. The old 2.x annotations from org.ops4j.pax.exam.testng are deprecated but still available.