C.2. Sample Pax-Exam Test Class
Sample test class Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Example C.2, “CalculatorITestClass” shows an example of how to write a test class for Apache Karaf in the Pax-Exam testing framework. The
CalculatorITest class configures the Apache Karaf environment, installs the obr and wrapper features, and then runs a test against the two features (where the obr and wrapper features implement particular sets of commands in the command console).
Example C.2. CalculatorITestClass
// Java
/*
* Licensed to the Apache Software Foundation (ASF)
* ...
*/
package org.ops4j.pax.exam.sample.karaf;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import java.io.File;
import javax.inject.Inject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.ConfigurationManager;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
import org.ops4j.pax.exam.options.MavenUrlReference;
import org.ops4j.pax.exam.sample8.ds.Calculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(PaxExam.class) 1
public class CalculatorITest {
private static Logger LOG = LoggerFactory.getLogger(CalculatorITest.class);
@Inject 2
protected Calculator calculator;
@Configuration 3
public Option[] config() {
MavenArtifactUrlReference karafUrl = maven()
.groupId("org.apache.karaf")
.artifactId("apache-karaf")
.version(karafVersion())
.type("zip");
MavenUrlReference karafStandardRepo = maven()
.groupId("org.apache.karaf.features")
.artifactId("standard")
.version(karafVersion())
.classifier("features")
.type("xml");
return new Option[] {
// KarafDistributionOption.debugConfiguration("5005", true),
karafDistributionConfiguration()
.frameworkUrl(karafUrl)
.unpackDirectory(new File("target", "exam"))
.useDeployFolder(false),
keepRuntimeFolder(),
configureConsole().ignoreLocalConsole(),
features(karafStandardRepo , "scr"),
mavenBundle()
.groupId("org.ops4j.pax.exam.samples")
.artifactId("pax-exam-sample8-ds")
.versionAsInProject().start(),
};
}
public static String karafVersion() {
ConfigurationManager cm = new ConfigurationManager();
String karafVersion = cm.getProperty("pax.exam.karaf.version", "3.0.0");
return karafVersion;
}
@Test 4
public void testAdd() {
int result = calculator.add(1, 2);
LOG.info("Result of add was {}", result);
Assert.assertEquals(3, result);
}
}
- 1
- The
@RunWithannotation instructs JUnit4 to run the following test with the PaxExam class. This is the key step for integrating JUnit4 with the Pax-Exam testing framework. - 2
- The
@Injectannotation is a javax annotation that identifies the following method as a service to be injected and executed in the testing framework. - 3
- The
@Configurationannotation is specific to Pax-Exam and marks the following method as a configuration method for setting Pax-Exam testing options. The configuration method must be declared aspublic staticand must have a return value of type,org.ops4j.pax.exam.Option[]. - 4
- The
@Testannotation is a standard JUnit4 annotation that identifies the following method as a test method to be executed in the testing framework.