Advanced JUnit usage

Advanced JUnit usage

Unknown macro: {scrollbar}

Configuration

Configuration is provided as a collection of org.ops4j.pax.exam.Option .
When using Exam with JUnit, you provide them by methods annotated with the @org.ops4j.pax.exam.Configuration annotation like so:

@Configuration
public static Option[] configure()
{
..
}

This Annotation allows to set a extend value of type Class .
Example

@Configuration ( extend=MyConfig.class )
public Option[] configure()
{
..
}

While MyConfig must extend CompositeOption to be recognized.
The, options specified in the "inherited" class are used included into this methods' configurationset.

Configuration is allowed on methods (as shown) as well as on the TestCase (Class) itself.
Annotating the TestCase with @Configuration ( extend=Some.class ) will include the configuration set in Some.class to all tests in this class.

Tests are marked as with the normal JUnit @Test annotation.
By default, any @Configuration will be combined into one configuration set for any @Test .

The following two variants are equivalent:

@Configuration
public static Option[] configure()
{
   return options(
      equinox(),
      webProfile()
   )
}
@Configuration
public static Option[] configure1()
{
   return options(
      equinox()
   )
}

@Configuration
public static Option[] configure2()
{
   return options(
      webProfile()
   )
}

However, you can use some more fine grained controls to match Configurations and Tests.

Fine grained configuration associations

First, there is the @AppliesTo annotation that limits the matching test.
The value specified is a regular expression matched against Test Method names.

The following configuration will be used for any test method starting with "foo".

@Configuration
@AppliesTo( { "foo.*" } )
public static Option[] rootConfig()
{
    return options(
        systemProperties( new SystemPropertyOption( "rootConfig" ).value( "true" ) )
    );
}

Options

To fine tune your setup while using JUnit you may make use of the JUnit specific options provided by org.ops4j.pax.exam.junit.JUnitOptions utility class. Using this you can specify by yourself that the JUnit & EasyMock related bundles to be included into the set of provisioned bundles as well as the version of this artifacts. By default (if not specified in the configuration) this bundles will be automatically included by Pax Exam.

Specify the JUnit bundles to be provisioned

...
import static org.ops4j.pax.exam.junit.JUnitOptions.*;
...
@Configuration
public static Option[] configureJUnitBundles()
{
    return options(
        junitBundles()
    );
}
...

Specify the version of JUnit bundles to be provisioned

...
import static org.ops4j.pax.exam.junit.JUnitOptions.*;
...
@Configuration
public static Option[] configureAnotherVersionOfJUnitBundles()
{
    return options(
        junitBundles().version( "4.5.0" )
    );
}
...

Specify the EasyMock bundles to be provisioned

...
import static org.ops4j.pax.exam.junit.JUnitOptions.*;
...
@Configuration
public static Option[] configureEasyMockBundles()
{
    return options(
        easyMockBundles()
    );
}
...

Specify the version of EasyMock bundles to be provisioned

...
import static org.ops4j.pax.exam.junit.JUnitOptions.*;
...
@Configuration
public static Option[] configureAnotherVersionOfEasyMockBundles()
{
    return options(
        easyMockBundles().version( "2.3.0" )
    );
}
...