【问题标题】:How to set Java/Scala tests' system properties through maven plugin?如何通过 maven 插件设置 Java/Scala 测试的系统属性?
【发布时间】:2015-10-09 21:53:55
【问题描述】:

我想使用 2 个不同的配置文件运行我的测试,每个配置文件都设置一个 Java 属性,导致我的 scala 测试代码以不同的方式执行。

我尝试配置 maven-surefire 和 maven-scalatest 插件:

        <plugin>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <systemProperties>
                    <spark.master>local</spark.master>
                </systemProperties>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <systemPropertyVariables>
                    <spark.master>local</spark.master>
                </systemPropertyVariables>
            </configuration>
        </plugin>

但似乎它们都不起作用,执行 System.getProperty("spark.master") 时结果仍然为空。我应该怎么做才能启用此设置?

//--------------------------------------------- --

对第一个答案的回应:

我已将我的 surefire 配置更改为以下内容:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <id>test</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <systemPropertyVariables>
                        <spark.master>${spark.master}</spark.master>
                    </systemPropertyVariables>
                </execution>
            </executions>
            <configuration>
                <forkCount>1</forkCount>
            </configuration>
        </plugin>

但显然是在错误的地方。 Maven 给了我这个错误:

[ERROR]     Malformed POM /home/peng/git/datapassport/pom.xml: Unrecognised tag: 'systemPropertyVariables' (position: START_TAG seen ...</goals>\n                        <systemPropertyVariables>... @170:50)  @ /home/peng/git/datapassport/pom.xml, line 170, column 50 -> [Help 2]

【问题讨论】:

    标签: java scala maven unit-testing properties


    【解决方案1】:

    (a) 如果您使用 JUnit - 将其版本升级到 4.7 或更高版本并指定显式提供程序:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-junit47</artifactId>
                <version>2.18.1</version>
            </dependency>
        </dependencies>
    

    (b) 在surefire插件配置中指定

    <forkCount>1</forkCount>
    

    (c) 使用插件的执行maven机制来运行两个不同的配置文件

    <executions>
        <execution>
            <id>tests-1</id>
            <goals><goal>test</goal></goals>
            <configuration>
                <systemProperyVariables ... />
            </configuration>
        </execution>
        <execution>
            <id>tests-2</id>
            <goals><goal>test</goal></goals>
            <configuration>
                <systemProperyVariables ... />
            </configuration>
        </execution>
    

    【讨论】:

    • 你确定这是 systemProperyVariables 应该在的地方吗?架构定义表明它不属于 。请看我上面的详细回复
    • 同时,我想知道这个解决方案是否也适用于 IDE 生成的测试?执行中的目标似乎表明它只适用于 mvn test
    • @tribbloid 我已将丢失的 标签添加到答案中。如果您从 IDE 运行 maven 目标 - 解决方案将起作用。如果您在 IDE 中运行单个测试 - 您应该在运行配置 JVM 选项中提供 -Dspark.master=
    • 非常感谢,我猜它是一个缺少功能的 IDE。我的测试证明了这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2012-01-08
    • 2016-02-07
    • 2013-01-07
    相关资源
    最近更新 更多