【问题标题】:"argline" argument not working in Maven Surefire Eclipse JUnit“argline”参数在 Maven Surefire Eclipse JUnit 中不起作用
【发布时间】:2016-07-15 07:51:15
【问题描述】:

在 Eclipse 中,我执行“运行方式”“Maven 测试”或“Maven 构建”,但测试失败。在“运行配置”中,我将目标设置为

清洁包 -DargLine="-Darg1=7070 -Darg2=9"

我也尝试过像这样通过 pom.xml 设置上述参数:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <argLine>-Darg1=7070 -Darg2=9</argLine>
                </configuration>
 </plugin>

我的 JUnit 测试用例是:

    public class TestCase1 {

    private static String arg1;
    private static String arg2;

    @Test
        public void testStart() throws Exception {
            System.out.println("arg1: " + arg1);
            System.out.println("arg2: " + arg2);
        }
    }

arg1 和 arg2 都为 null。

如果有人能帮忙,谢谢。

【问题讨论】:

  • 测试从不使用程序参数。并且 arg1 和 arg2 从未初始化,因此它们为空。
  • 测试使用这些参数,我刚刚发布了代码以显示它们为空。这些不应该通过 pom.xml 初始化吗?我的意思是,arg1 不应该是 7070 而 arg2 不应该是 9 吗?如果没有,那么从 pom.xml 初始化它们的方法是什么?
  • 您不是在获取参数,而是仅检查未初始化变量的值,这些变量当然是 null

标签: java eclipse maven junit maven-surefire-plugin


【解决方案1】:

不会自动获取这些字段。您需要致电System.getProperty(String name)

private static String arg1 = System.getProperty("arg1");
private static String arg2 = System.getProperty("arg2");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多