java - 如何通过maven插件设置Java/Scala测试的系统属性?

标签 java scala maven unit-testing properties

我想使用 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]

最佳答案

(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) 使用plugun的执行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>

关于java - 如何通过maven插件设置Java/Scala测试的系统属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047695/

相关文章:

java - 我该如何编写这个评分系统?

java - 如何用Java收集AMD CPU Cache Hit Ratio?

java - 从 IDE 在终端中运行 java 程序时出现 NoClassDefFoundError

scala - 根据条件拆分 Spark DataFrame

java - 使用 dockerfile 中的外部库编译并运行 java

scala - 更新 Scala 案例类的操作

scala - 递归创建scala中字符串的所有旋转

maven - 无法从 springsource 中的原型(prototype)创建项目

java - Maven 在/WEB-INF/lib 文件夹中构建损坏的 jar 文件

java - 在maven中编译问题