java - PAX 考试 - @Before 方法未调用

标签 java junit4 pax-exam

我正在尝试使用 PAX Exam 设置测试,如下所示:

@ExamReactorStrategy(PerMethod.class)
public class AbstractTest {

    @Configuration
    public Option[] config() {
        return options(
                junitBundles(),

                /* PAX Logging */
                mavenBundle("org.ops4j.pax.logging", "pax-logging-api", "1.7.2"),
                mavenBundle("org.ops4j.pax.logging", "pax-logging-service", "1.7.2"),

                /* Apache Felix Config Admin */
                mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.0"),

                /* Eclipse Gemini dependencies */
                mavenBundle().groupId("org.aopalliance").artifactId("com.springsource.org.aopalliance").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.aop").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.beans").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.context").versionAsInProject(),
                mavenBundle().groupId("org.springframework").artifactId("org.springframework.core").versionAsInProject(),

                /* Eclipse Gemini */
                mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-core", GEMINI_VERSION),
                mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-extender", GEMINI_VERSION),
                mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-io", GEMINI_VERSION),

                /* Other bundles */;
    }

    @Before
    public void setUp() throws Exception {
        ....
    }

}


@RunWith(PaxExam.class)
public class MyTest extends AbstractTest {

    @Inject
    private MyObject myObject;

    @Test
    public void testOne() {
        ...
    }

}

由于某种原因,使用@Before注释的方法没有被调用。

谢谢,迈克尔

<小时/>

编辑:我使用的 PAX EXAM 依赖项是:

<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-container-native</artifactId>
    <version>${pax.exam.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-junit4</artifactId>
    <version>${pax.exam.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-link-mvn</artifactId>
    <version>${pax.exam.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.url</groupId>
    <artifactId>pax-url-aether</artifactId>
    <version>${url.version}</version>
    <scope>test</scope>
</dependency>

我使用 PAX EXAM 版本 3.4.0。

最佳答案

我已经设置了一个测试项目并且运行良好。

我终于找到了导致问题的原因。看来测试的执行在配置过程中停止了。

我使用 Eclipse Gemini 2.0.M02,并在应用程序上下文中使用占位符。例如,我需要将数据源 URL 设置为属性。我想使用 @Before 方法通过配置管理服务设置 PID 的配置。

由于应用程序上下文是在配置期间加载的,并且此时 PID 尚未配置(因为尚未调用 @Before 方法),因此我从 Spring 收到错误,该错误导致 PAX EXAM 中止当前测试的执行。

因此,在我的案例中,我无法通过 @Before 方法使用配置管理服务。

解决方案是使用 pax-exam-cm 模块,该模块允许在配置方法内与配置管理服务进行交互,如下所示:

@Configuration
public static Option[] config() {
    return options(
            junitBundles(),

            /* PAX Logging */
            mavenBundle("org.ops4j.pax.logging", "pax-logging-api", "1.7.2"),
            mavenBundle("org.ops4j.pax.logging", "pax-logging-service", "1.7.2"),

            /* Apache Felix Config Admin */
            mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.0"),

            ConfigurationAdminOptions.newConfiguration("my.pid")
                .put("prop1", "value1")
                .put("prop2", "value2")
                .asOption(),

            /* Eclipse Gemini + dependencies */
            mavenBundle().groupId("org.aopalliance").artifactId("com.springsource.org.aopalliance").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.aop").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.beans").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.context").versionAsInProject(),
            mavenBundle().groupId("org.springframework").artifactId("org.springframework.core").versionAsInProject(),
            mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-core", GEMINI_VERSION),
            mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-extender", GEMINI_VERSION),
            mavenBundle("org.eclipse.gemini.blueprint", "gemini-blueprint-io", GEMINI_VERSION),

            /* Other bundles */
            );
}

您还需要将 pax-exam-cm 依赖项添加到您的 POM(如果您使用 Maven):

<dependency>
    <groupId>org.ops4j.pax.exam</groupId>
    <artifactId>pax-exam-cm</artifactId>
    <version>3.4.0</version>
    <scope>test</scope>
</dependency>

关于java - PAX 考试 - @Before 方法未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22130252/

相关文章:

java - ExecuteStreamCommand 不会读取外来字符

java - Play 2.3.x 框架不在子项目中运行 JUnit 测试

java - 将使用插件的Maven项目转换为Gradle项目

java - Pax Exam + Karaf容器注入(inject)自定义服务遇到ClassNotFoundException

java - 使用 CSVBeanReader 忽略不需要的列

java - Java中临时变量的效率

java - 通过 spring 显示多表 jasper 报告

java - 为什么 Selenium 在每个 JUnit 测试项之间创建一个新的 RC 和/或浏览器实例?

java - 将 Maven 测试日志重定向到 Eclipse 控制台

maven - PaxExam OSGI 容器和 ServiceLookupException