如何将输入从命令行传递到JUnit Maven测试程序 [英] How to pass input from command line to junit maven test program

查看:70
本文介绍了如何将输入从命令行传递到JUnit Maven测试程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个junit测试,将两个数字相加.我需要从命令行传递此数字.我正在从Maven工具运行此junit测试,

I wrote a junit test to add two numbers. I need to pass this numbers from command line. I am running this junit test from maven tool as

mvn -Dtest=AddNumbers

我的测试程序看起来像这样

My test program looks like this

int num1 = 1;
int num2 = 2;

@Test
public void addNos() {
  System.out.println((num1 + num2));
}

如何从命令行传递这些数字?

How to pass these numbers from command line?

推荐答案

将数字作为@artbristol建议的系统属性进行传递是一个好主意,但是我发现并不总是保证将这些属性传播到测试.

Passing the numbers as system properties like suggested by @artbristol is a good idea, but I found that it is not always guaranteed that these properties will be propagated to the test.

要确保将系统属性传递给测试,请使用 maven surefire插件 argLine 参数,例如

To be sure to pass the system properties to the test use the maven surefire plugin argLine parameter, like

mvn -Dtest=AddNumbers -DargLine="-Dnum1=1 -Dnum2=2"

这篇关于如何将输入从命令行传递到JUnit Maven测试程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆