How to run JUnit from command line with vm arguments? - java

Is there any way to run JUnit test cases with program arguments, VM arguments and working directory?
I've tried with org.junit.runner.JUnitCore, but I can't find the way to pass these parameters.

You can pass system parameters with the -Dproperty=value
Program arguments are just passed at the end of the java command line. However, they will be passed to Junit's main method. so you cannot use them in your test class. If you want to use some parameter in your test class, set a system parameter and retrieve it with System.getProperty(String) in your code.
So the command line would look like this (you probably need to set other options like classpath):
java -Dfile.encoding=UTF-8 -Dmy.param=foo org.junit.runner.JUnitCore org.package.MyTest
For the working directory I would change to the desired directory with cd before invoking JUnit.

Related

Override default log location for JMeter when launching with Java (not command line)

Is there an equivalent of the -j command-line option when launching JMeter using Java?
I'm running a series of test scripts that each need to have a custom log name - normally I'd just define each log location using -j, but in these cases they need to be launched through Java.
If you have possibility to amend the java code and invoke System.setProperty function :
System.setProperty("jmeter.logfile", "/desired/path/to/jmeter.log)"
If you don't:
either pass the property via -D command line argument like:
java -Djmeter.logfile=/desired/path/to/jmeter.log ....
or add the next line to system.properties file:
jmeter.logfile=/desired/path/to/jmeter.log
More information:
JMeter Properties Reference
Apache JMeter Properties Customization Guide
Overriding Properties Via The Command Line

passing argument to Runtime.getRuntime().exec() with an option at the beginning

I'm trying to pass a command like the following to Runtime.getRuntime().exec() where the option should be at the beginning.
option="something" ./test.sh a b
Whatever I'm trying, I get this exception:
Cannot run program "option="something"": error=2, No such file or directory
Any ideas?
Doing option="something" only works in shells to set an environment variable for this specific process. To do this in java you must use one of the overloads of exec that take environment variables as a parameter.
Such as:
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[])
or use ProcessBuilder:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Edit:
You should note that explicitly setting environment variables for the new process, causes all environment variables in the current program to be ignored. If you want to pass the current environment variables you must include them yourself.

How to pass parameters to jUnit not from Ant (in Eclipse)?

I would like to pass some parameters to jUnit while testing. I know, for example, that it can be set showoutput to 'yes'.
How to do this from command line or from Eclipse run configuration?
Params can be passed to Java like this:
java com.example.foo.bar -Dparam1=val1 -Dparam2=val2
So, running jUnit and passing params works in the same manner, create a new configuration, and pass the params as VM options.
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore -Dparam1=val1 [test class]

what's the purpose of using run following a main class on the java command line?

I am seeing some code are started like
java MainClass run -cp ******
I have two questions here,
why using run
why specify another classpath following run, any reason or benefit?
In your example, "run" is only an argument of the program, not the JVM. It is not a keyword or anything. The program will simply be passed it as a String.
The -cp argument seems to be also an argument to the program.
From the look of the command line, I guess the MainClass program is used to run another class, which is looked for in the classpath defined by the -cp argument. So, neither "run" nor "-cp ..." are actually used by the VM to run MainClass, but by MainClass itself to run another program.
run, -cp, and ****** will be passed as arguments to the main function in the java class MainClass.
Anything on the command-line, after the class name, are arguments to the class, not arguments to the java VM.

Giving environment variables as arguments

I have a jar file that gets arguments from commandline and I want to give parameter that contains environment variable. Something like below:
java -jar MyDev.jar -f %PROJECT_HOME%/src/test
But in above case program creates a directory named %PROJECT_HOME% however I want that PROJECT_HOME value in system is /home/jack path. And program should follow /home/jack/src/test not %PROJECT_HOME%/src/test path.
How can I do that ?
Are you running this in a Unix shell? If so, I suspect you just want:
java -jar MyDev.jar -f ${PROJECT_HOME}/src/test
Using % is the Windows way of specifying environment variables - which doesn't appear to fit with a home directory of /home/jack...
The component responsible for environment variables substitution is the shell/command line processor (cmd.exe on Windows).
I wrote the following main method:
public static void main(String[] args) {
System.out.println(args[0]);
}
When I pass "%PATH%" as an argument, running it from within Eclipse prints out %PATH%. Running it from the command line prints out the actual path environment variable.
Note that you can access environment variables from your Java code by using System.getenv().
For example, System.out.println(System.getenv("PATH")) prints out the actual path variable both from Eclipse and from the command line.
One very likely cause for this could be that the variable PROJECT_HOME is not defined or has a misspelled name. Hence, unless you have already done so, you should do echo %PROJECT_HOME% right before you start the java program in order to ensure that the variable is defined.

Categories

Resources