TestNG Driver

For a much improved version of the TestNG driver, you should upgrade to Pax Exam 3.x.

Pax Exam >= 2.3.0

Since Pax Exam 2.3.0, there is a TestNG driver which is rather limited compared to vanilla TestNG, but still equivalent to the functionaliy of the JUnit driver in previous Pax Exam releases.

Here is an example of what you can do with the current Pax Exam release:

public class SampleTest {

    @Inject
    private BundleContext bc;

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

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

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

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

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

Points to note

  • TestNG has no direct equivalent to the @RunWith extension mechanism of JUnit. TestNG lets you customize its behaviour by listeners which can be configured by annotation or by the JDK ServiceLoader via META-INF/services definitions.
  • TestNG does not currently provide any hooks for intercepting @Before and @After methods (this includes all levels of @BeforeMethod, @BeforeClass etc.). Until this issue is addressed in TestNG, Pax Exam support for TestNG will be limited to the same level available for JUnit in Pax Exam 2.2.0, i.e. test methods will be intercepted by the driver and delegated to the container, whereas before and after methods are not supported at all. More precisely, before and after methods are run by the driver and not by the container, as most users would expect.
    See PAXEXAM-28 for a discussion and further references.
  • The @ExamReactorStrategy(...) annotation lets you specify a reactor strategy. It is optional and defaults to @ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
  • 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.
  • Test methods are identified by the usual @org.testng.annotations.Test annotation.
  • Pax Exam annotations @Configuration, @ExamReactorStrategy etc. are currently duplicated between the JUnit and TestNG drivers. They should be refactored into a single package, but this would break the released JUnit driver API, so we need to define a deprecation strategy first.

You currently need to provision TestNG explicitly:

mavenBundle("org.testng", "testng", "6.3.1"),

Pax Exam <= 2.2.0

Pax Exam 2.2.0 does not support TestNG. Please upgrade to the current release or use the JUnit Driver instead.