Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass input from command line to junit maven test program

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?

like image 666
Achaius Avatar asked Mar 28 '12 06:03

Achaius


People also ask

Is it possible to pass command line arguments to a test execution in JUnit?

Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below.

How do you pass an argument to a JUnit test?

You don't need to pass command line parameters to your JUnit execution. Instead your test methods should build/prepare everything and call your new myClass(...) constructor with the parameters your original program would do when using command line parameters.


1 Answers

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.

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" 
like image 182
FrVaBe Avatar answered Sep 18 '22 06:09

FrVaBe