我正在尝试用pax-maven-构建构建我的OSGI,同时用pax考试测试它。它提供了一些包,我无法用下面的pax- test配置进行测试:
@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class OSGILoaderTest {
@Inject
protected BundleContext bundleContext;
@Configuration
public Option[] config() throws MalformedURLException {
String projectRoot = // a path to my project
return options(
junitBundles(),
equinox(),
bundle(projectRoot + "libs/org.eclipse.core.variables_3.2.500.v20110511.jar"),
bundle(projectRoot + "libs/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar"),
bundle(projectRoot + "libs/org.eclipse.core.expressions_3.4.300.v20110228.jar"),
// etc...
);
}
@Test
public void getBundleContext() throws RodinDBException {
IRodinDB rodinDB = RodinCore.getRodinDB();
assertNotNull(rodinDB);
}
}在这里,我可以从一个附带条件的jar中访问IRodinDB实例。
现在我有了自己的包代码,它将使用提供的所有jar。但是我甚至不能测试我自己的代码,例如:
@Test
public void checkAccessToRodinDbTest() {
VTGService service = null;
assertTrue(true);
}在编译时给出一个错误,
错误未能执行目标org.ops4j:maven-pax-plugin:1.5:testCompile (默认-testCompile):编译失败 错误找不到符号 错误符号:类VTGService
测试编译似乎不能看到'src/main/java',这与maven编译器插件的默认行为相反。但是在我的例子中,您可以看到maven并不使用编译器插件,而是使用maven-pax-plugin。
问题是:我如何用pax考试测试我自己的包?
update1
这似乎是最近版本的maven- Pax -plugin的一个问题,因为ops4j pax插件中可用的基本示例 (在POM中使用Pax插件的部分)似乎也遇到了同样的问题。
update2
按照Dmytro的要求,这是my的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<relativePath>../poms/compiled/</relativePath>
<groupId>fr.xlim.ssd.vtg.build</groupId>
<artifactId>compiled-bundle-settings</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<properties>
<bundle.symbolicName>fr.xlim.ssd.vtg.bundle</bundle.symbolicName>
<bundle.namespace>fr.xlim.ssd.vtg.bundle</bundle.namespace>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>fr.xlim.ssd.vtg</groupId>
<artifactId>fr.xlim.ssd.vtg.bundle</artifactId>
<version>0.1-SNAPSHOT</version>
<name>${bundle.symbolicName}</name>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<type>pom</type>
<groupId>${project.parent.groupId}</groupId>
<artifactId>provision</artifactId>
<optional>true</optional>
</dependency>
<!-- not needed as equinox bundle are available in provision -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_compendium</artifactId>
<optional>true</optional>
</dependency-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-inject</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-mvn</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>我不确定这是最优雅的解决方案,但我创建了一个新的maven项目,当我可以导入我自己的包时,就像在我问题的源代码中一样。
是否有一种优雅的方法可以直接将我自己的java源代码作为新包添加到同一个Maven项目中进行测试?这可能是不可能的(因为捆绑汇编操作是在之后编译和测试完成的).
发布于 2012-04-12 12:09:26
我使用下面的设置来提供测试中的包。在配置测试时,我使用reference-protocol提供包(这是Equinox和Felix的一个非标准特性,参见这里):
@Configuration
public Option[] config() {
return options(
bundle("reference:file:target/classes"),
junitBundles(),
felix()
);
}当您指定knopplerfish()作为环境时,测试用例也会运行。我猜这是因为URL是由Pax考试解决的,而不是由OSGi运行时解析的。我使用maven-bundle插件构建包。要使此工作如愿以偿,您必须添加以下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<executions>
<!-- This execution makes sure that the manifest is available
when the tests are executed -->
<execution>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>否则,在运行测试时,清单将不可用,因为默认情况下,清单是在package-phase期间生成的。
我希望我没有忘记任何事情-请告诉我,如果它对你有用!
发布于 2012-04-12 09:00:03
检查PaxExam文档如何使用PaxExam配置Maven POM。
样本这里
https://stackoverflow.com/questions/10120277
复制相似问题