2012-03-23 94 views
3

我使用pax-exam來加載,激活和訪問osgi包。BundleContext在使用pax-exam的單元測試中爲null

以下源代碼是我的pax考試考試,它使用本機容器使用pax-exam 2.3運行。

package fr.xlim.ssd.vtg.osgi; 

import java.net.URISyntaxException; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.ops4j.pax.exam.Inject; 
import org.ops4j.pax.exam.Option; 
import org.ops4j.pax.exam.junit.Configuration; 
import org.ops4j.pax.exam.junit.ExamReactorStrategy; 
import org.ops4j.pax.exam.junit.JUnit4TestRunner; 
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory; 
import org.osgi.framework.BundleContext; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import static org.junit.Assert.assertNotNull; 
import static org.ops4j.pax.exam.CoreOptions.bundle; 
import static org.ops4j.pax.exam.CoreOptions.junitBundles; 
import static org.ops4j.pax.exam.CoreOptions.options; 

@RunWith(JUnit4TestRunner.class) 
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class) 
public class BasicOSGILoaderTest { 

    private Logger logger = LoggerFactory.getLogger(BasicOSGILoaderTest.class); 

    @Inject 
    private BundleContext bundleContext; 

    @Configuration 
    public Option[] config() { 

    String projectRoot = null; 

    try { 
     projectRoot = ClassLoader.getSystemResource(".").toURI().toURL().toString(); 
     projectRoot = projectRoot.toString().substring(0,projectRoot.toString().length() - 27); 
     logger.debug("project root: {}", projectRoot); 
    } catch (MalformedURLException e) { 
     logger.error("malformed URL to project root: {}", projectRoot, e); 
     throw new IllegalStateException(e); 
    } catch (URISyntaxException e) { 
     logger.error("URI Syntax Error to project root: {}", projectRoot, e); 
     throw new IllegalStateException(e); 
    } 

    return options(
     junitBundles(), 
     logProfile(), 
     bundle(projectRoot + "libs/org.eclipse.ant.core_3.2.300.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"), 
     bundle(projectRoot + "libs/org.eclipse.core.jobs_3.5.100.v20110404.jar"), 
     bundle(projectRoot + "libs/org.eclipse.core.resources_3.7.100.v20110510-0712.jar"), 
     bundle(projectRoot + "libs/org.eclipse.core.runtime_3.7.0.v20110110.jar"), 
     bundle(projectRoot + "libs/org.eclipse.equinox.app_1.3.100.v20110321.jar"), 
     bundle(projectRoot + "libs/org.eclipse.equinox.common_3.6.0.v20110523.jar"), 
     bundle(projectRoot + "libs/org.eclipse.equinox.preferences_3.4.1.R37x_v20110725.jar"), 
     bundle(projectRoot + "libs/org.eclipse.equinox.registry_3.5.101.R37x_v20110810-1611.jar"), 
     bundle(projectRoot + "libs/org.eventb.core_2.4.0.r14093.jar"), 
     bundle(projectRoot + "libs/org.eventb.core.ast_2.4.0.r14093.jar"), 
     bundle(projectRoot + "libs/org.eventb.core.seqprover_2.4.0.r14093.jar"), 
     bundle(projectRoot + "libs/org.rodinp.core_1.5.0.r14093.jar"), 
     equinox().version("3.7.1") 
    ); 
    } 

    @Test 
    public void checkBundleContext1() { 
     assertNotNull(bundleContext); 
    } 

    @Test 
    public void checkBundleContext2(BundleContext bc) { 
     assertNotNull(bc); 
    } 

    @Test 
    public void checkAccessToRodinDB() { 
     RodinCore.getRodinDB(); 
    } 
} 

但我已經存在以下問題:

  • 兩個checkBundleContext方法失敗,包上下文注入(使用@Inject),或爲測試方法的參數傳遞總是空

  • RodinCore類別的包中獲得靜態方法getRodinDb()的套件org.rodinp.core_1.5.0.r14093.jar拋出一個ClassNotFoundExceptionorg.rodinp.core.RodinCore,即使套件org.rodinp.core位於軟件包的導出包中。

回答

6

檢查您的導入。使用

import javax.inject.Inject; 

Service Injection page

注射液是由javax.inject.Inject註解來表示。 Pax 考試自己的1.x版本註釋現在註冊爲 。

+0

已經存在,我已經更新代碼源以方便查看。 – Kartoch 2012-03-23 11:39:04

+0

[link](http://team.ops4j.org/wiki/display/paxexam/Service+Injection)注入由javax.inject.Inject註釋表示。 – Borbas 2012-03-23 12:00:49

+0

YEAH!你說得對:正確的解決方案是使用'javax.inject.Inject'。 – Kartoch 2012-03-23 12:23:37

2

檢查依賴性:

<groupId>org.ops4j.pax.exam</groupId> 
<artifactId>pax-exam-inject</artifactId> 

應該在PAX-考試測試包括在內。

更多詳情here

+0

已添加,但仍無法正常工作:assertNotNull拋出AssertionError with bundleContext使用'@ Inject'註釋 – Kartoch 2012-03-23 11:45:45

+0

您能提供您的POM嗎? – 2012-03-23 12:22:40

相關問題