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

Achaius picture Achaius · Mar 28, 2012 · Viewed 30.9k times · Source

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?

Answer

FrVaBe picture FrVaBe · May 4, 2012

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"