JAR Probe Assembly

JAR Probe Assembly

Since 3.5.0.

Automatic Defaults

In CDI mode, Pax Exam by default uses the test class path, creating a bean archive for each class path element containing a META-INF/beans.xml descriptor.

Manually Assembling a JAR Probe

Sometimes, it is not desirable to have all classes on the test class path to be treated as beans. In this case, you can create a JAR probe which contains an explicit subset of classes and resources. Pax Exam creates a JAR from these resources on the fly, builds an URL class loader for this JAR and sets this loader as thread context class loader before starting the CDI container.

Using a configuration method and a jarProbe() option with additional arguments, you can assemble the JAR probe from scratch:

package org.ops4j.pax.exam.regression.cdi.probe.calc.test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.ops4j.pax.exam.CoreOptions.jarProbe; import static org.ops4j.pax.exam.CoreOptions.options; import javax.inject.Inject; import org.junit.Test; import org.junit.runner.RunWith; import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.junit.PaxExam; import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; import org.ops4j.pax.exam.spi.reactors.PerClass; @RunWith(PaxExam.class) @ExamReactorStrategy(PerClass.class) public class AdditionTest { @Inject private Calculator calculator; @Configuration public Option[] config() { return options(jarProbe() .classes(Addition.class, BinaryOperation.class, Calculator.class, CalculatorImpl.class) .metaInfResource("src/test/resources/calc/beans.xml") ); } @Test public void add() { assertThat(calculator.operate(2, 3), is(5)); } }

The above example creates a JAR containing nothing but four classes and a META-INF/beans.xml resource with the given content. 

jarProbe() Arguments

classes

  • Adds classes to WEB-INF/lib.

    classes(Vanilla.class, Chocolate.class)

metaInfResource

  • Adds the given resource from the class path to the WAR in META-INF/.

    metaInfResource("src/test/resources/calc/foo") // copies the given resource to META-INF/foo

resources

  • Adds the given resources from the current class path to the JAR, using the same relative path.

     resources("com/resources("com/acme/Messages.properties", "com/acme/Messages_de.properties")