Ach*_*ius 31 junit junit4 maven
我写了一个junit测试来添加两个数字.我需要从命令行传递这个数字.我从maven工具运行这个junit测试
mvn -Dtest=AddNumbers
Run Code Online (Sandbox Code Playgroud)
我的测试程序看起来像这样
int num1 = 1;
int num2 = 2;
@Test
public void addNos() {
System.out.println((num1 + num2));
}
Run Code Online (Sandbox Code Playgroud)
如何从命令行传递这些数字?
Vla*_*roz 17
要将输入从命令行传递到junit maven测试程序,请按照以下步骤操作.例如,如果您需要将参数fileName传递给Maven执行的单元测试,请按照以下步骤操作:
在JUnit代码中 - 参数将通过System属性传递:
@BeforeClass
public static void setUpBeforeClass() throws Exception {
String fileName = System.getProperty("fileName");
log.info("Reading config file : " + fileName);
}
Run Code Online (Sandbox Code Playgroud)在pom.xml中 - 在surefire插件配置中指定param名称,并使用{fileName}表示法强制maven从System属性中获取实际值
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- since 2.5 -->
<systemPropertyVariables>
<fileName>${fileName}</fileName>
</systemPropertyVariables>
<!-- deprecated -->
<systemProperties>
<property>
<name>fileName</name>
<value>${fileName}</value>
</property>
</systemProperties>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)在命令行中将fileName参数传递给JVM系统属性:
mvn clean test -DfileName=my_file_name.txt
Run Code Online (Sandbox Code Playgroud)art*_*tol 12
您可以在命令行上传递它们
mvn -Dtest=AddNumbers -Dnum1=100
然后在测试中访问它们
int num1=Integer.valueOf(System.getProperty("num1"));
| 归档时间: |
|
| 查看次数: |
26469 次 |
| 最近记录: |