7

I have recently been using junit in eclipse and I am still learning. I know how to pass command line parameters in eclipse, but how do I pass them to a test case in Junit? Also how do I access them?

Virat Kadaru
  • 2,216
  • 4
  • 23
  • 28

4 Answers4

14

You cannot pass command line arguments to the JUnit test because no main method is run. You will need to use system properties and access these in your test case.

Select your test class in the Package Explorer view. Right click and select Run As -> Open Run Dialog In the run dialog window there is an Arguments tab where you can specify Program arguments and VM arguments. You should be able to enter your system property parameters here.

Alternatively, with the desired project as your current one, from the main menu select Run -> Run Configurations to access the Arguments tab.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Mark
  • 28,783
  • 8
  • 63
  • 92
  • But if I use eclipse, I do not need a main method to run my test. How do I access them without setting them in system properties? – Virat Kadaru Mar 26 '09 at 00:04
  • It seems that with JUnit4 and Eclipse your only option is system properties. I have edited my answer to reflect this. – Mark Mar 26 '09 at 09:35
  • 1
    This is another option: http://stackoverflow.com/questions/14820175/how-to-pass-an-argument-to-a-android-junit-test/14821971#14821971 – Diego Torres Milano Feb 11 '13 at 22:20
6

I will skip passing as somebody has already replied with that. To access you use:

System.getProperty("propert.name.here");

(returns String)

Cukierpudel
  • 61
  • 1
  • 1
  • Not only does this just bloat the code, but it doesn't even address the actual question. (Who voted this up anyway?) – arkon May 29 '12 at 03:21
  • 1
    This snippet of code shows how to get at the system property that @Mark suggested be used as a way to pass an argument from the Eclipse Junit launch configuration into the JUnit test. In my case, I'm interested in doing that with a port number for example. – Dave Apr 15 '13 at 17:25
3

In this example I am passing an argument webDriver as firefoxDriver in the run configuration window:

Example

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
Doni Tie
  • 31
  • 3
2

Probably you have figured this out, but when compiled and if using ANT or MVN, you can pass arguments to your JUNIT or TestNG from inside the POM.XML file.

 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.4.3</version>
  <configuration>
    <forkMode>${test.junit.forkMode}</forkMode>
    <skip>${test.junit.skip}</skip>
    <argLine>${test.junit.argLine}</argLine>
    <jvm>${jdk.compiler.path}/binjava</jvm>
  </configuration>
</plugin>
Amir
  • 21
  • 1