OSGI集成测试,Pax Exam探针在容器捆绑包启动前执行测试



我目前正试图使用PAX-EXAM+KARAF编写OSGI集成测试,但遇到了一个问题,即PAX-EXAM试图在容器中实际启动/初始化依赖捆绑包之前执行测试方法。奇怪的是,有时测试会成功,在这种情况下,所有捆绑包/上下文都会启动并记录,但大多数情况下不会。方法上的延迟不会有帮助:(有人可以帮助解决这个问题吗?

我正在使用PAX检查2.6.0,org.apache.karaf.tooling.exam容器2.3.0,apache karaf 2.3.0。

代码:

@Inject
BundleContext bundleContext;
@Inject
    EntityManagerFactoryService entityManagerFactoryService;//Service exposed trough OSGI
    protected EntityManager entityManager;
    @Before
    public void init() throws InterruptedException {
        entityManager = entityManagerFactoryService.getEntityManagerFactory().createEntityManager();
    }
@Configuration
    public static Option[] configuration() throws Exception {
    return new Option[] {
            karafDistributionConfiguration().frameworkUrl(maven().groupId("org.apache.karaf").artifactId("apache-karaf").type("zip").versionAsInProject())
                    .karafVersion("2.3.0").name("Apache Karaf"),                
mavenBundle("com.google.guava", "guava", "13.0.1").startLevel(30),
mavenBundle("com.mysql.jdbc", "com.springsource.com.mysql.jdbc", "5.1.6").startLevel(30),
                mavenBundle("javax.persistence", "com.springsource.javax.persistence", "2.0.0").startLevel(30),
                mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.lang", "2.6.0").startLevel(30),
...the rest of bundles  
junitBundles(), };

测试方法:

@Test
    public void contextNotNull() {
        Assert.assertNotNull(entityManagerFactoryService);
    }

日志:

java.lang.ClassNotFoundException: com.startjg.crp.core.dao.service.EntityManagerFactoryService not found by PAXEXAM-PROBE-749fa717-8bdc-4d9a-9803-bdaf6d4edac0 [144]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1460)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

完整日志:https://www.dropbox.com/s/v12r15sbmtu9svp/log.txt

也尝试了没有运气:

protected <T> Object getService(Class<T> serviceClass) {
    int maxCount = 5;
    int delay = 5000;
    for (int i = 0; i <= maxCount; i++) {
        if (bundleContext.getServiceReference(serviceClass) != null) {
            ServiceReference<T> serviceReference = bundleContext.getServiceReference(serviceClass);
            return bundleContext.getService(serviceReference);
        } else {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
@Before
public void init() throws InterruptedException {
    EntityManagerFactoryService emfs = (EntityManagerFactoryService) getService(EntityManagerFactoryService.class);
    entityManager = entityManagerFactoryService.getEntityManagerFactory().createEntityManager();
}

您确定您的EntityManager可以作为服务运行吗?您很可能缺少一些依赖项,这就是为什么包含服务的捆绑包没有完全启动的原因。

当前的pax考试3.3.0现在完全支持karaf作为一个容器。以前的karaf考试代码现在完全转移到pax考试。

最新更新